The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 06:37 AM
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 04:15 AM
Find matching lines between 2 files jojojmac5 UNIX for Dummies Questions & Answers 5 01-18-2007 01:06 PM
Grep all files matching partial filename mharley Shell Programming and Scripting 3 06-08-2005 02:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-05-2007
smb_uk smb_uk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 7
Returning filename and matching lines

What I'm trying to do is to search through a list of files, and output the filename, followed by the lines that matched the pattern.

I'm matching the string "letters.moreletters" in any one of searched files, and the output I'm trying to get is:

program_1.txt
10 dsdsd sdsd dsd dsdsd.dsdsd dsdsdsd sdsd
12 sasas sasas sasas sasas.sasas sasas sasas
program_2.txt
15 ewewe ewewe ewewe ewewe.ewew ewewe
25 fdfdf fdfdf fdfdf fdfdf.fdfdf fdfdf dfdfdf fdfdf

So far I've got
grep -n '[A-Za-z0-9_]\.[A-Za-z_]' *.txt | awk -F: '{print $1"\n";print $2"\t"$3}'
(apologies if thats not the exact syntax, I can't test at the moment) which outputs :
program_1.txt
10 dsdsd sdsd dsd dsdsd.dsdsd dsdsdsd sdsd
program_1.txt
12 sasas sasas sasas sasas.sasas sasas sasas
program_2.txt
15 ewewe ewewe ewewe ewewe.ewew ewewe
program_2.txt
25 fdfdf fdfdf fdfdf fdfdf.fdfdf fdfdf dfdfdf fdfdf

...which is nearly there, but I don't know UNIX well enough to know if I'm going in the wrong direction. The other problem with the above command is it assumes the : will be used for the grep delimiter, and that no more will appear.
I basically want awk to take the first, second and the rest of the string, and dedupe every third line.

Any help is much appreciated, as there appears to be so many ways to accomplish these things, but putting them to practise is another story.
  #2 (permalink)  
Old 10-05-2007
atripat atripat is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 7
Here is one quick and simple way:

Code:
for file in `grep -l aabbcc *`
        do
        echo $file
        grep -n aabbcc $file | sed 's/:/ /'
        done
Replace aabbcc in the code above with the pattern you are looking for in the file.
Replace the first * in the code with your file name pattern.
sed will only look for the first instance of : on the output from grep.
  #3 (permalink)  
Old 10-05-2007
smb_uk smb_uk is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 7
That's great, although I'm hoping to find a way that works without a shell, so for instance, I could have it working in Windows with the UnxUtils.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0