Skip to main content
Tweeted twitter.com/#!/StackUnix/status/447281219127242752
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k
deleted 331 characters in body
Source Link
branquito
  • 1k
  • 1
  • 9
  • 17

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.

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.

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" }
  '
}

, 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.

added 335 characters in body
Source Link
branquito
  • 1k
  • 1
  • 9
  • 17

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.

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" }
  '
}

, 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.

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.

added 62 characters in body
Source Link
branquito
  • 1k
  • 1
  • 9
  • 17
Loading
formatting
Source Link
manatwork
  • 32.1k
  • 8
  • 104
  • 93
Loading
Source Link
branquito
  • 1k
  • 1
  • 9
  • 17
Loading