![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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 the first * in the code with your file name pattern. sed will only look for the first instance of : on the output from grep. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|