awk search and get top record


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk search and get top record
# 1  
Old 05-04-2018
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

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

# 2  
Old 05-04-2018
Try this:-

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

Here it what I've tried:-

Code:
n]$ cat a1.out
$Temp=mannu

n]$ cat a2.out
$Temp=jskobs

Code:
$  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)

Code:
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.

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


Last edited by Mannu2525; 05-04-2018 at 05:07 AM..
# 3  
Old 05-04-2018
Top record (first occurrence?) from each file or first occurrence from search of all files?

Last edited by rdrtx1; 05-04-2018 at 03:30 PM..
# 4  
Old 05-07-2018
It should be first occurrence from search of all files..
Problem with my command: returning 2nd field of all search results...
# 5  
Old 05-07-2018
Code:
awk -F"=" '(/\$Temp/ && ++found==1) {print $2}' /DIRECTORY/*

The ==n is universal: the nth match.
# 6  
Old 05-07-2018
Another idea, very efficient, is to exit after the print.
Code:
awk -F"=" '/\$Temp/ {print $2; exit}' /DIRECTORY/*

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Record top accessed processes/files

Hello, I have about 100 servers that I'm looking to collect information regarding top files and processes accessed within a 168 hr (1 week) period. Each server has a different purpose and so different installed applications. All servers are running either unix or linux. What would be a... (0 Replies)
Discussion started by: umang2382
0 Replies

2. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

3. Shell Programming and Scripting

Search and Replace by record position

Hi All, I have a file that I would like to search for data and replace other data by record position number: Example search.. search for "CLARK KENT" and replace Amt data "000025" with "000155"??? I'm able to search and replace unique data but, came to a stump when wanting to replace data... (11 Replies)
Discussion started by: macastor
11 Replies

4. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

5. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

6. Shell Programming and Scripting

How to search for a pattern from bottom to top.

Hi, I have a file, which is having a pattern "SEARCH" somewhere towards end of the file, if i am giving " grep -i "SEARCH" $File" , it is taking too much time as file is very big. So i want to search for the pattern from the back side of the file, how can we search for a pattern in bottom... (5 Replies)
Discussion started by: Prat007
5 Replies

7. Shell Programming and Scripting

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (2 Replies)
Discussion started by: shilendrajadon
2 Replies

8. UNIX for Advanced & Expert Users

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (1 Reply)
Discussion started by: shilendrajadon
1 Replies
Login or Register to Ask a Question