1

I use awk to parse a file. Have stored pattern words in an awk array. Would like to do something like this.

if ( $0 ~ / arr[1] / ){
blah
}

I want to check if the pattern stored in the array element is found in the current line which is being parsed.

heemayl
  • 91,753

1 Answers1

1

Just use the array accessing afterwards, no need for the // as then the arr[1] will be taken as a Regex pattern, do:

if ( $0 ~ arr[1] ){ blah }

Example:

% awk 'BEGIN{a[1]="foo"} {if ($0 ~ /a[1]/) print "Matched"}' <<<'foobar'  
% awk 'BEGIN{a[1]="foo"} {if ($0 ~ a[1]) print "Matched"}' <<<'foobar' 
Matched
heemayl
  • 91,753