How can I pass variables from awk to a shell command?

Vahid Mirjalili picture Vahid Mirjalili · Dec 18, 2013 · Viewed 92.4k times · Source

I am trying to run a shell command from within awk for each line of a file, and the shell command needs one input argument. I tried to use system(), but it didn't recognize the input argument.

Each line of this file is an address of a file, and I want to run a command to process that file. So, for a simple example I want to use 'wc' command for each line and pass $1to wc.

awk '{system("wc $1")}' myfile

Answer

Kent picture Kent · Dec 18, 2013

you are close. you have to concatenate the command line with awk variables:

awk '{system("wc "$1)}' myfile