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.

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

```text
 and 
```

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

 grep "xxx" *datei* /dev/null 

Guess you just need the file name with content include some key words, use -l option in grep:

grep -l tree file*
$> grep -o three file*
file1:three
file1:three
file2:three

If your grep does not have -o etc. and this is regarding to your former post Searching Problem, 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:

$> 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