Monday, May 6, 2024
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 1865  / 2 Years ago, sat, june 4, 2022, 9:02:39

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?


More From » command-line

 Answers
6

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.


[#2871] Sunday, June 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ularousand

Total Points: 380
Total Questions: 109
Total Answers: 101

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;