[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [RFC] Exim as standard Debian MTA?




On Fri, 28 Aug 1998, Stephen J. Carpenter wrote:

> Well PERL is just such a nice language....
> I do need to learn more perl...but then again...
> I think I want to leanr sed and awk first (currently even I don't regularly use
> much more than grep for filtering...time to learn...)

you can grep with sed and awk if you want to practice a little:
Instead of

  grep "foo" bar

use:

  sed -ne "/foo/p" bar

or:

  awk "/foo/" bar

The latter being shorthand for:

  awk "/foo/ { print }" bar

in turn being shorthand for (notice the ' preventing the shell
from expanding $0 ):

  awk '/foo/ { print $0 }' bar

You can also print only the second and fourth word of every line matching
the regular expression "foo":

  awk '/foo/ { print $2 " " $4 }' bar

this can be done with sed as well:

  sed -ne '/foo/s/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\).*/\2 \4/p' bar

but it looks really horrible.  It is horrible.

or print the amount of lines with more than five words that matched "foo":

  awk '/foo/ && NF > 5 { 
            print "here'\''s one, on line " NR " !" ; 
            n++ 
       }
       END { 
            print "matched " n " times.\n" 
       }' bar

Of course, at this point, you should consider perl, as it is generally
faster than awk and far more featureful.  Right now, there is a breakeven
in codesize:

  perl -ne 'if (/foo/ && split > 5) { 
                 print "here'\''s one, on line $. !\n";
                 $n++ 
            }
            END {
                  print "matched $n times.\n" 
            }' bar 

or:

  perl -e 'while (<>) {
               if (/foo/ && split > 5) {
                    print "here'\''s one, on line $. !\n";
                    $n++
               }    
           }
           print "matched $n times.\n" ' bar

You can actually do a lot with awk that one would believe requires perl,
but in most cases, perl does offer the cleaner and more extensible
implementation.  And it's faster too.  And it has a -w switch. 

Cheers,


Joost


Reply to: