Allan Pinto
Allan Pinto

Reputation: 144

search for variable in multiple files within the same script

i have a script which reads every line of a file and outputs based on certain match,

function tohyphen (o) {
    split (o,a,"to[-_]")
    split (a[2],b,"-")
    if (b[1] ~ / /) { k=""; p=""; }
    else { k=b[1]; p=b[2] }
    if (p ~ / /) { p="" }
    return k
}

print k, "is present in" , FILENAME

what i need to do is check if the value of k is present in say about 60 other files and print that filename and also it has to ignore the file which it was original reading, im currently doing this with grep , but the calling of grep so many times causes the cpu to go high, is there a way i can do this within the awk script itself.

Upvotes: 1

Views: 108

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85845

You can replace your pipeline grep "$k" *.cfg | grep "something1" | grep "something2" | cut -d -f2,3,4 with the following single awk script:

awk -v k="$k" '$0~k&&/something1/&&/something2/{print $2,$3,$4}' *.cfg

You mention printing the filename in your question, in this case:

awk -v k="$k" '$0~k&&/something1/&&/something2/{print FILENAME;nextfile}' *.cfg

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77145

You can try something like this with gnu awk.

gawk '/pattern to search/ { print FILENAME; nextfile }' *.files

Upvotes: 1

Related Questions