Returning filename and matching lines

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning filename and matching lines
# 1  
Old 10-05-2007
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:

10 dsdsd sdsd dsd dsdsd.dsdsd dsdsdsd sdsd
12 sasas sasas sasas sasas.sasas sasas sasas
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 :
10 dsdsd sdsd dsd dsdsd.dsdsd dsdsdsd sdsd
12 sasas sasas sasas sasas.sasas sasas sasas
15 ewewe ewewe ewewe ewewe.ewew ewewe
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  
Old 10-05-2007
Here is one quick and simple way:

for file in `grep -l aabbcc *`
        echo $file
        grep -n aabbcc $file | sed 's/:/ /'

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  
Old 10-05-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning specific columns upon matching

Hi All, Need help in this requirement. I have fileA with one column and fileB with 26 columns. I need to match the value from fileA with fileB, if matches I have to return that value from fileB, and the next value, 5th and 6th values. NOTE- the matching value's position changes in... (7 Replies)
Discussion started by: vamsikrishna928
7 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Returning two lines if they both match strings

Hi I have a problem where I have a large amount of files that I need to scan and return a line and its following line, but only when the following line begins with a string. String one - line one must begin with 'Bill' String two - line two must begin with 'Jones'. If these two... (7 Replies)
Discussion started by: majormajormajor
7 Replies

4. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

5. UNIX for Dummies Questions & Answers

Help with filename pattern matching

Hi, I have files in a directory with filenames that match three specific patterns: 1) *'.L2_LAC'* 2) *'.L2_LAC_OC'* 3) *'.L2_LAC_SST'* I would like to have an "ls" command that will list only files matching the first two patterns. However, if I type: ls *'.L2_LAC'* I will get files that... (2 Replies)
Discussion started by: msb65
2 Replies

6. Shell Programming and Scripting

loop and extract matching filename

i am unable to exact matching filename in the loop. file contains Customer_Product_ Information_Customer_Product_ sale_Product_ /home_dir contains files CUST_INFO_990090_1111.csv "1","Customer Product Detail","1000","salary" "1","Information Customer Product... (2 Replies)
Discussion started by: onesuri
2 Replies

7. UNIX for Dummies Questions & Answers

Pattern matching and Printing Filename

Hi, My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present. Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Discussion started by: krao
4 Replies

8. Shell Programming and Scripting

Shell script: returning the file with the most lines

Hey I am relatively new to Linux and shell scripting, looking for a spot of help with a script I am working on. I am writing a script that counts the number of lines in all the files in a directory, sorts them by line number and then returns ONLY the file with the most lines. Right now I can... (11 Replies)
Discussion started by: Breakology
11 Replies

9. Shell Programming and Scripting

Filename and Pattern matching

Hiiiii every one, I am facing a problem while giving a file name which has space in it. The problem is ... I have to read a file where the set of input files are stored. Now getting that name I have to open the file and i have to extract the name of the user from where it is written like"by... (2 Replies)
Discussion started by: kheyali Mitra
2 Replies

10. Shell Programming and Scripting

Grep all files matching partial filename

What would be the easiest way to grep all files within a particular directory that match a partial filename? For example, searching all files that begin with "filename.txt" and are appended with the date they were created. I am using Ksh 88, btw. (3 Replies)
Discussion started by: mharley
3 Replies
Login or Register to Ask a Question