Having the following in one of my shell functions:
function _process () {
awk -v l="$line" '
BEGIN {p=0}
/'"$1"'/ {p=1}
END{ if(p) print l >> "outfile.txt" }
'
}
addition for clarification purposes:
while read line
do whois $line | awk -v l="$line" -v search=$1 'BEGIN {p=0}; /$0 ~ search/ {p=1}; END{ if(p) print l >> "outfile.txt" }'
done < infile.txt
,with infile.txt having:
2aeonacademiccollege.com
abbopticalgroup.net
charm-vision.com
, so when called as _process $arg, $arg gets passed as $1, and used as a search pattern. It works this way, because shell expands $1 in place of awk pattern! Also l can be used inside awk program, being declared with -v l="$line". All fine.
Is it possible in same manner give pattern to search as a variable?
Following will not work,
awk -v l="$line" -v search="$pattern" '
BEGIN {p=0}
/search/ {p=1}
END{ if(p) print l >> "outfile.txt" }
'
,as awk will not interpret /search/ as a variable, but instead literally.