For example,
$cat > test.txt
aa bb cc
11 22 33
44 55 cc
or
$awk 'BEGIN{cmd="grep cc test.txt | sort -k1,1 | head -n1 | cut -f2 -d\" \""; cmd | getline a; print a}'
55
$awk 'BEGIN{cmd="grep cc test.txt | sort -k1,1 | head -n1 | cut -f2 -d\" \""; system(cmd);}'
55
Note that if only use cmd, it won't print out anything, because cmd itself won't switch to console (unlike system).
$awk 'BEGIN{cmd="grep cc test.txt | sort -k1,1 | head -n1 | cut -f2 -d\" \""; cmd;}'
If you want to output the multiple lines and process them in awk, you can do
Reference: http://stackoverflow.com/questions/1960895/awk-assigning-system-commands-output-to-variable
$awk 'BEGIN{cmd="grep cc test.txt | sort -k1,1 | cut -f2 -d\" \""; while( (cmd | getline a) >0) print a;}'
55
bb
Reference: http://stackoverflow.com/questions/1960895/awk-assigning-system-commands-output-to-variable
No comments:
Post a Comment