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.

  • I want to check if the pattern stored in the array element is found in the current line. – Andromeda64 Nov 18 '16 at 20:55
0

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
  • @andromeda64 No problem. Don't forget to mark the answer as accepted. – heemayl Nov 18 '16 at 21:06
  • I did, but since I am new, it has recorded the feedback but would not show to public. – Andromeda64 Nov 18 '16 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.