awk search and get top record

im trying to search for a keyword called ($Temp) in a DIRECTORY, get the top record from this search, and print 2nd field.
here is my command, can you plz let me know what is wrong

awk -F"=" '/\$Temp/ NR==1{print $2}' /DIRECTORY/*

Try this:-

awk -F"=" 'BEGIN {NR==1} /\$Temp/{print $2}' /DIRECTORY/*

Here it what I've tried:-

n]$ cat a1.out
$Temp=mannu

n]$ cat a2.out
$Temp=jskobs
$  awk -F"=" 'BEGIN {NR==1} /\$Temp/{print $2}'  n/*
mannu
jskobs

OR use this for multiple $Temp occurrences (but first occurrence should be at the top)

awk -F"=" '/\$Temp/ {if(FNR==1) {print $2}}'  n/*

OR

Not to worry about the occurrence. If it's there in the file at any position, below command will extract the first one from the file and so on for rest of the files in the directory.

find /dir/ -exec grep -m 1  '\$Temp' {} \; | awk -F"=" '{print $2}'

Top record (first occurrence?) from each file or first occurrence from search of all files?

It should be first occurrence from search of all files..
Problem with my command: returning 2nd field of all search results...

awk -F"=" '(/\$Temp/ && ++found==1) {print $2}' /DIRECTORY/*

The ==n is universal: the nth match.

Another idea, very efficient, is to exit after the print.

awk -F"=" '/\$Temp/ {print $2; exit}' /DIRECTORY/*
1 Like