2

I am using Ubuntu 16.04.

I want to search for a certain word, but I also want the whole file that contains the word to be outputted on the terminal so that I can see where in the file the word is located. However, when I use grep, it will only show the particular sentence that contains the word, not the whole file.

What command can I use to search for a word but also show the whole file along with the place where the word is located?

8

With grep you can use:

$ grep -E -e 'pattern' -e '$' file

or in a simplified form:

$ grep -E 'pattern|$' file

Technically this would work too:

$ grep -E 'pattern|^' file

or even : grep -E 'pattern|' file. It says lines with pattern or no pattern at all. so everything will match.

I have grep aliased to grep --color=always, so if you don't you have to pass the color option yourself.


With less:

$ less -p 'pattern' file

So the file would pop-up in less, the desired term would be highlighted and you can use n and shift+n to jump around between matches.

| improve this answer | |
  • 1
    The question asks for all lines of matching files. This will show all lines of all files (except empty files). – Barmar Aug 10 at 14:43
  • Thanks for the comment. But from what I can see, question asks "How to search for a word in a file". – Ravexina Aug 11 at 7:01
  • "I also want the whole file that contains the word to be outputted". So if the file doesn't contain the word, it shouldn't be output. – Barmar Aug 11 at 7:04
  • If you're showing the whole file unconditionally, what's the point of searching for the word? – Barmar Aug 11 at 7:05
  • As it's only one file, It pops-up, you take a peek... either your pattern is there or not. However doesn't less -p do exactly what you are concern about? – Ravexina Aug 11 at 7:15
4

Edit: I misinterpreted the question in my original answer.... I have edited it to produce the desired result... I was only showing the file name in the output. I edited this answer to use that output to show the entire file by using less (as Ravexina's answer suggests)
...in addition, this will only return the files (in less) that have been matched by grep when searching through a directory(s).
....and I added -I to ignore binary files, they may make the less interaction messy. Remove it if you need to search binary files or files that grep may "think" are binary.

Use the "with-filename" flag with grep to show the file. (the flag is -H)

I am assuming you are combing through a directory and each of the files in them. If searching outside of current directory, use the * to prevent "is a directory" error with no results.

grep -HI pattern /directory/* | less -p pattern $(awk -F ":" '{print $1}')

or

grep --with-filename -I pattern /directory/* | less -p pattern $(awk -F ":" '{print $1}')

or if drilling down into sub-directories...

grep -rHI pattern /directory/* | less -p pattern $(awk -F ":" '{print $1}')

I don't like entering the pattern twice, it could use improvement

| improve this answer | |
  • Did you misunderstand the question? This adds the filenames to the output, it doesn't show all the lines of the files. – Barmar Aug 10 at 14:44
  • @Barmar yeah you are correct, I did misunderstand the question... I have edited my answer – WU-TANG Aug 10 at 22:32
2

If you want to see where in the file your searched text is use C flag. For example:

grep -C 5 'MySearchText' *

will show the line containing the text and five lines before and five lines after the intended search.

| improve this answer | |
0

To complement the other answers: you can add '-n' to the options to see the line numbers on the left:

grep -n -C 3  "something"  filename

will show you lines containing "something", with 3 lines of "Context" (ie, 3 lines before and after the one(s) matching "something"), and each shown lines will have its line number appended at the beginning.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.