[Solved] Grep multiple files and display first match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Grep multiple files and display first match
# 1  
Old 02-10-2014
[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help!
# 2  
Old 02-10-2014
Code:
awk 's != FILENAME && /pattern/{print; s=FILENAME}' file1 file2 ....

with gawk:
Code:
gawk '/pattern/{print; nextfile}' file1 file2 ....

# 3  
Old 02-10-2014
That works perfectly! Is there a way to make it display the file name as well as the first matched line?
# 4  
Old 02-10-2014
Quote:
Originally Posted by dbiggied
That works perfectly! Is there a way to make it display the file name as well as the first matched line?
Sure.
Code:
awk 's != FILENAME && /pattern/{print FILENAME; print; s=FILENAME}' file1 file2 ....

Or with gawk:
Code:
gawk '/pattern/{print FILENAME; print; nextfile}' file1 file2 ....

# 5  
Old 02-10-2014
Like this ?

Code:
$ grep -m 1 'pattern' file*

# 6  
Old 02-10-2014
Quote:
Originally Posted by Akshay Hegde
Like this ?

Code:
$ grep -m 1 'pattern' file*

Be aware that the -m option is not available on all systems.
# 7  
Old 02-10-2014
Quote:
Originally Posted by Franklin52
Sure.
Code:
awk 's != FILENAME && /pattern/{print FILENAME; print; s=FILENAME}' file1 file2 ....

Or with gawk:
Code:
gawk '/pattern/{print FILENAME; print; nextfile}' file1 file2 ....

How would I modify this to place both the name of the file and the matched line on the same line in the output? perhaps separated by a colon or something?




Quote:
Originally Posted by Franklin52
Be aware that the -m option is not available on all systems.
My system does not have the -m option.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep and display multiple lines

Hi guys, I have a log file that generates multiple logs about a query. <query time='2016-04-13 13:01:50.825'> <PagingRequestHandler> <Before>brand:vmu</Before> <After>brand:vmu</After> </PagingRequestHandler> <GroupDeviceFilterHandler> <Before>brand:vmu</Before> ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

2. Shell Programming and Scripting

Match multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

3. Shell Programming and Scripting

[Solved] Multiple input files and output files

Hi, I have many test*.ft1 files to which I want to read as input for a script called pipe2txt.tcl and print the output in each separate file. For example, pipe2txt.tcl < test001.ft1 > test001.txt How can I read many files in this maner? thank you very much, Best, Pahuja (5 Replies)
Discussion started by: Pahuja
5 Replies

4. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

5. Shell Programming and Scripting

[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2). Mapping for pattern match and add line : for Alpha 123 for Beta 234 for Gamma 345 no match (goes into another file) File 1 ... (3 Replies)
Discussion started by: w020637
3 Replies

6. Shell Programming and Scripting

shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries. Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line. Input file contains. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

7. Shell Programming and Scripting

[solved] Diff between two files by grep

My requiremeny is as follows, I have two files file a A BONES RD,NHILL,3418,VIC 37TH PARALLEL RD,DEEP LEAD,3385,VIC 4 AK RD,OAKEY,4401,QLD A & J FARRS RD,BARMOYA,4703,QLD A B PATTERSON DR,ARUNDEL,4214,QLD A BLAIRS RD,BUCKRABANYULE,3525,VIC file b A BONES... (12 Replies)
Discussion started by: feelmyfrd
12 Replies

8. UNIX for Dummies Questions & Answers

compare two files if doesnt match then display error message

hi , i have one file ,i need to search particular word from this file and if content is matched then echo MATCHED else NOT MATCHED file contains : mr x planned to score 75% in exam but end up with 74%. word to be searched id 75% please help me out . waiting for reply thanks in advance (2 Replies)
Discussion started by: atl@mav
2 Replies

9. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

10. Shell Programming and Scripting

grep on multiple words to match text template

hi, I have few text templates as a simple ex: template 1 city Name: zip code: state Name: template2: employee Name: Phone number: I wish to grep on given text file and make sure the text file matches one of these templates. Please give your ideas. (6 Replies)
Discussion started by: rider29
6 Replies
Login or Register to Ask a Question