4

I need an egrep command that list all the file names that contains the words + which string it has found. Let's imagine this scenario:

Words that I need to find: apple, watermelon and banana.

What I need: I want to list all files that contains any of them (don't need to have all of them in the same file), and print which word it found in the file. Example of a search result I want:

./file1.txt:apple
./file1.txt:banana
./file2.txt:watermelon
./file5.txt:apple

I remember seeing a grep command which shows FILENAME:STRING in the search results, but I can't remember how it was done. I tried:

egrep -lr 'apple|banana|watermelon' .

But the search results showed:

./file1.txt
./file2.txt

Ok, it helps but ... in file1, which one of the words it found? That's the problem I'm facing .

0

4 Answers 4

15

You used -l, which is the opposite of what you want. From man grep:

-l, --files-with-matches
      Suppress normal output; instead print the  name  of  each  input
      file  from  which  output would normally have been printed.  The
      scanning will stop on the first  match.   (-l  is  specified  by
      POSIX.)

What you want is -H:

-H, --with-filename
      Print the file name for each match.  This is  the  default  when
      there is more than one file to search.

But that's the default anyway. Just do:

grep -Er 'apple|banana|watermelon' .

(The -E tells grep to act like egrep.)

4
  • Thanks! It's almost what I want ... using -Er, it's showing the Filename and the whole line with the word I want ... but I want to print just the word it found, not the whole line containing the word. Jul 1, 2015 at 19:33
  • 2
    @user2576376 Just use grep -Ero 'apple|banana|watermelon' .
    – heemayl
    Jul 1, 2015 at 19:38
  • It returns "grep: illegal option -- o" ... I'm running these comands in an AIX server, tell me if there is any way to show you the version of the system or anything that could help you identify, I'm really a begginer on it. Jul 1, 2015 at 19:40
  • 5
    @user2576376 Then you're asking this on the wrong site. Ask on Unix & Linux.
    – muru
    Jul 1, 2015 at 19:45
4

The way to do it with awk:

awk -v var1=apple -v var2=banana -v var3=watermelon '{ if($0~var1) {print FILENAME":"var1} ; if($0~var2) {print FILENAME":"var2} ; if($0~var3) {print FILENAME":"var3}  }' *

Basically, declare 3 variables, and 3 if statements to print filename and corresponding variable that has been found

EDIT

Shorter version:

awk '/watermelon/{ print FILENAME":watermelon" }; /banana/{print FILENAME":banana"}; /apple/ {print FILENAME":apple"}' *

Basic idea find /regular expression/ and execute code in curly braces after it if found (which is to print FILENAME and which string we found ).

6
  • Thanks for the help :-), but the result of it is just showing the file name, and not the word it found within this file. Jul 1, 2015 at 19:35
  • @user2576376 I see , I thought you only wanted the filenames. I'll fix Jul 1, 2015 at 19:37
  • Thanks @Serg, it worked like a charm! One last question: is there a way to filter the files it will look? Example: I want to find these 3 words in all files that the filename contains *.doc ... if it's not possible that's ok, I'll just remove the ones I don't want from the result :-) Jul 1, 2015 at 20:21
  • @user2576376 .doc files somehow differ from simple text files, so just running a simple application like grep or awk on them wont work. They're encoded somehow that makes them binary files. So if I run less somefile.doc it asks permission to view it , since it may be binary file, and there's nothing but special characters there. Jul 1, 2015 at 20:44
  • @user2576376 added a shorter version, please check Jul 1, 2015 at 21:32
3

Here is the command you're looking for:

grep -R "apple\|banana\|watermelon" <search_path>

The -R will read all files under each directory in search_path, recursively. The file name will be displayed for each match.

1
  • How can I execute this command in the current directory? I tried: grep -R "apple\|banana\|watermelon" ., but it did not return any result. Jul 1, 2015 at 19:56
2

The following will give you the output you want (using bash):

for x in apple banana watermelon ; do grep $x * ; done

The output will give you all lines containing the search string.

The following will give you only the search string as output:

for x in apple banana watermelon ; do grep -l $x * |xargs -I{} echo {}:$x; done
2
  • Thanks for the help, but it gives me the whole line content --> Filename:the fruit is apple... what I need is, for example: Filename:apple Jul 2, 2015 at 18:13
  • @sanjuro8998 I've edited the answer to give you only the search string as output
    – NZD
    Jul 2, 2015 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .