Question

I Have a series of files and would like to know which ones meet a certain criteria (egs. $1 == "ice" && $2 == "cream"). This criterion is met several times in the same file. I would like awk to print the filename once when this criterion is met.

I hope I made myself clear.

Was it helpful?

Solution

Try this (nextfile is a GNU extension):

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

Or if you 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 the p flag at the start of each file, and then when your criteria are met, the p flag gets set to 1 so the filename only comes out once.

By the way, you may prefer the simplicity of this:

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

It is not identical, but pretty similar.

EDITED AFTER ACCEPTANCE

This is another possibility that may be more elegant. It saves the names of matching files in an array names[] and prints the keys of the array names[] at the end.

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

OTHER TIPS

This awk should do

awk '$1 == "ice" && $2 == "cream" {print FILENAME}' *

To get line number and line info, you can use:

awk '$1 == "ice" && $2 == "cream"  {print NR,$0,"->",FILENAME}' *

Another version:

awk '/^ice cream/ {print NR,$0,"->",FILENAME}' *
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top