Visit Our UNIX and Linux User Community


find file and print only contents with a hit by grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find file and print only contents with a hit by grep
# 1  
Old 10-22-2009
find file and print only contents with a hit by grep

Hi,

can someone help me.

I have some files and search a content in this files. If i have a hit I will print a output:

filename:content

But are more hits in one file: The output is always filename:content

E.G. Seach about "three"

file1 {one, two, three, four, three}
file2 {three, five, six, seven}
file3 {seven, eight}

Output:
file1:three
file1:three
file2:three


I search a unix statement which makes this output. I searching without success for a week now.
THANKS.
# 2  
Old 10-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 10-22-2009
Code:
 grep "xxx" *datei* /dev/null

# 4  
Old 10-22-2009
Guess you just need the file name with content include some key words, use -l option in grep:

Code:
grep -l tree file*

# 5  
Old 10-22-2009
Code:
$> grep -o three file*
file1:three
file1:three
file2:three

# 6  
Old 10-23-2009
If your grep does not have -o etc. and this is regarding to your former post https://www.unix.com/shell-programmin...g-problem.html, you can also just slightly modify the line I already gave in the there to get only the matches in the files, not the complete files as output:

Code:
$> cat file1
eins
zwei
OCT
drei
vier
$> cat file2
16-SEP-2009 09:11:47 10.65.4.24
16-SEP-2009 09:11:47 10.3.4.11
30-SEP-2009 10:11:47 10.3.4.11
1-OCT-2009 10:11:47 10.65.4.24
6-OCT-2009 10:11:47 10.3.4.11
6-OCT-2009 12:31:01 10.3.4.11
16-OCT-2009 11:11:47 10.65.4.24
17-OCT-2009 11:11:47 10.65.4.24
18-OCT-2009 11:11:47 10.3.4.11
$> find . -type f -exec grep -l OCT {} \; | while read LINE; do awk -v l=$LINE '/OCT/ {print l":"$0}' $LINE; done
./file2:1-OCT-2009 10:11:47 10.65.4.24
./file2:6-OCT-2009 10:11:47 10.3.4.11
./file2:6-OCT-2009 12:31:01 10.3.4.11
./file2:16-OCT-2009 11:11:47 10.65.4.24
./file2:17-OCT-2009 11:11:47 10.65.4.24
./file2:18-OCT-2009 11:11:47 10.3.4.11
./file1:OCT


Previous Thread | Next Thread
Test Your Knowledge in Computers #227
Difficulty: Easy
According to NetMarketShare, in September 2019 BSD had a 0.01% global market share of the desktop computing market.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash command to find a file and print contents

I need to find a file and print its contents I am trying but it is not working find -path /opt/app-root/src/.npm/_logs -type f -name "*.log" -print Version $ bash -version GNU bash, version 4.4.12(1)-release (x86_64-pc-msys) (1 Reply)
Discussion started by: SVRao19056
1 Replies

2. Shell Programming and Scripting

How to grep and print the next and previous N lines after the hit

Hello, I know that gnu grep has option of -A and -B to extra previous and next lines. But I'm using HP UX and its grep does not support these options. BID="0/0/6/1/1.145.17.255.0.0.0" I need to search a file using $BID and get next 5 lines and previous 5 lines separately. Please... (1 Reply)
Discussion started by: reddyr
1 Replies

3. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

4. UNIX for Dummies Questions & Answers

Print file name when running grep from within find

Solaris 10 When running grep from within find command (don't know the technical term for 'running from within' ) , find command returns only the line which contains the pattern. Is there any way to get the file name printed as well ? $ pwd /opt/testdir/anotherDir $ $ $ cat findme.txt... (3 Replies)
Discussion started by: omega3
3 Replies

5. Shell Programming and Scripting

script to read the contents of a file and print

Hi, Need help in writing a script to read the contents of this file test Test 00a 00b 00c 00d 00e 00f where it need to read each line to give a display such as form meta from dev 00a , config=Striped; add dev 00b:00f to meta 00a Can any one help me in writing this script (2 Replies)
Discussion started by: praveen1516
2 Replies

6. Shell Programming and Scripting

Read contents of the file and print

AT ---------- 0 Elapsed: 00:00:00.02 SO ---------- 0 Elapsed: 00:00:00.01 SE ---------- 0 Elapsed: 00:00:00.01 CR ---------- (4 Replies)
Discussion started by: sandy1028
4 Replies

7. UNIX for Dummies Questions & Answers

find file grep it and print file name

i am trying to search a few hundred release note text files for a certain word. however when i use the below command i can find a file that contains it but i dont know the file name. how can i change this command to output the name of the file that grep was successful in? find builds -name... (4 Replies)
Discussion started by: borderblaster
4 Replies

8. Shell Programming and Scripting

print contents of any file within some timeframe

Hi, Is there anyway to print contents of any file ( say log files that grow automatically) within some timeframe ( comparing with current time), say print contents of the added in: 1) last 2 hr 2) last 45 min 3) last 3 hrs 47 min (3 Replies)
Discussion started by: fed.linuxgossip
3 Replies

9. Shell Programming and Scripting

awk - print file contents except regex

Hello, I have a file which has user information. Each user has 2 variables with the same name like Email: testuser1 Email: testuser1@test.com Email: testuser2 Email: testuser2@test.com My intention is to delete the ones without the '@' symbol. When I run this statement awk '/^Email:/&&!/@/'... (6 Replies)
Discussion started by: rmsagar
6 Replies

10. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

Featured Tech Videos