Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

doing awk like stuff in perl

Posted by peeterjoot on August 19, 2009

I’ve seen people use awk many times to grab a field or two from some command output (example: $1 for the pid from ps output), but never remember the syntax, so I usually do this harder ways, with stuff like sed or a perl script that uses split.

That’s a bit of a pain if you are just doing a line liner throw away command. For that example above of how to grab the second field from command output, here’s how to do it

somecommand | perl -ane 'print "$F[1]\n;"'

the -e option is to specify the script inline, -n says wrap the script in an implied ‘while () { … }’ to process all of stdin (behave like a filter). The new trick is the -a which says that as well as putting in the auto-filter behaviour, run split and save the output in @F.

This can be combined with the -F flag to specify a split delimiter. An example is:

perl -anF/ -e '$n=scalar(@F) - 1 ; print "$F[$n]" ;' < ListOfChangedFiles.txt

This grabs all the trailing filename paths, like a loop of basename calls in this one liner ksh command:

for i in `cat ListOfChangedFiles.txt ` ; do basename $i ; done

It’s not any easier to do it in perl, but there’s more flexibility (you could use more than just the last field since they’ll all be in the array @F). What I really wanted was a diff of all the files with some context. I can do that in the same command, with something like:

perl -anF/ -e '$x=$F[@F-1] ; chomp $x ; system "diff -wpU10 $x.orig $x";' < ListOfChangedFiles.txt  | tee ~/tmp/diff.out

The chomp was because perl was actually including the newline in $F[$n] of the previous command. Of course like many things in perl, if you stand back and look from a distance it looks like ASCII barf. Like a dog, any Unix programmer should be able to appreciate downing a choice selection of regurgitation every once in a while.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: