awk + bash = power
I wanted to call this “advanced awk”, but it’s in fact simple idea with awk and bash integration. I got this idea from wonderful ArchLinux forums.
The problem is as follows. Given a file with several columns, i want to run a command on some columns and keep other columns as is.
For example, given a file with user names and some more information, i need to convert user names to user ids and keep other information unchanged. Example file is:
The desired result is:root a b c
ftp d e f
0 a b c(of course, user id of ‘root’ is 0 and of ‘ftp’- 14)
14 d e f
Well, there’s a way to do this
cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}'And now, only thing left to do is to execute the commands awk created for us - i.e. pipe to bash
The final command is:
cat myFile | awk '{print "echo \"$(id -u "$1") " $2 " " $3"\""}' | bash
Enjoy!

