1. 程式人生 > >[awk]awk 與 shell的引數傳遞(雙向)

[awk]awk 與 shell的引數傳遞(雙向)

1) 從bash傳遞匹配引數給awk:

printf "Enter search pattern: "

read pattern
awk "/$pattern/ "'{ nmatches++ }
     END { print nmatches, "found" }' /path/to/data


或:


printf "Enter search pattern: "
read pattern
awk -v pat="$pattern" '$0 ~ pat { nmatches++ }

       END { print nmatches, "found" }' /path/to/data


https://www.gnu.org/software/gawk/manual/html_node/Using-Shell-Variables.html


2) 從awk傳遞多個結果賦值給bash變數: /*採用eval或source*/

$z=`echo cpe:/o:yggdrasil:linux |awk -F ":" '{print "vendor="$3; print "product="$4;}'`;eval $z; echo $vendor; echo $product
yggdrasil
linux


http://www.theunixschool.com/2012/08/awk-passing-awk-variables-to-shell.html