Perl/etc. on the Command Line

  • No need to put Perl commands in a script file before running them. Write quick Perl scripts on the command line, possibly inside backquotes (`...`).
  • Example: search all files, show lines containing cat and dog and bird:
    % perl -ne 'print if /cat/ && /dog/ && /bird/' *
    The dog ate the cat, which had eaten the bird.
    The birds flew away from the dogged cat.
  • Example: print all those files (use command substitution):
    % lpr `perl -ln0e 'print $ARGV if /cat/ && /dog/ && /bird/' *`
  • Other UNIX text processing utilities (sed, awk, etc.) work the same way.
    What do the following examples do?
    1. ls -l | awk '{print $1, $8}'
    2. ls | sed "s@^@`pwd`@"
    3. sed G report | lpr
    4. sed 'G;G' report | lpr
    5. perl -pe 'print "\n\n"' report | lpr
< previous  index  next >

Contact Jerry Peek