Friday, March 14, 2003

Shell escape to AWK

Yep, I use Vi as an editor and am always looking for new tricks. As such, I'll include them here unless I notice sudden upswing in sales of pitchforks and torches to the villagers.

Following is a neat trick for pulling a document through awk from inside of Vi. Say you generate a file by typing:

   ls -l > myfile

"myfile" then contains like:

   -rw-rw-r-- 1 joat joat 610 Oct 29 10:28 whois

You can then generate a list of shell commands by typing:

   :1,.!awk '{print "cp",$9,$9 ".bak"}'

An alternative to this is:

   :%!awk '{print "cp",$9,$9".bak"}'

This takes the ninth field in each row and inserts it into an output line with the format of

   cp whois whois.bak

Source: UNIX IN THE ENTERPRISE newsletter for 13 March 2003.

No comments:

Post a Comment