if statement - awk: printing FILENAME once when criteria are met several times in a file -


i have series of files , know ones meet criteria (egs. $1 == "ice" && $2 == "cream"). criterion met several times in same file. awk print filename once when criterion met.

i hope made myself clear.

try (nextfile gnu extension):

awk '$1=="ice" && $2=="cream"{print filename;nextfile}'  file1 file2 file3 

or if don't have gnu:

awk 'fnr==1{p=0} $1=="ice" && $2=="cream" && !p {print filename;p=1}'  file1 file2 file3 

the fnr==1 resets p flag @ start of each file, , when criteria met, p flag gets set 1 filename comes out once.

by way, may prefer simplicity of this:

grep -l "^ice.*cream" file* 

it not identical, pretty similar.

edited after acceptance

this possibility may more elegant. saves names of matching files in array names[] , prints keys of array names[] @ end.

awk '$1=="ice" && $2=="cream" {names[filename]++} end{for(i in names)print i}' 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -