Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux search for word and show entire line

Tags:

linux

grep

find

I have a very large log file (6 gig).

I want to search for 'Nov 12 2012' and print off each line.

I'm a linux novice and have no idea how this is done. Mostly likely will need a more option to view X number of lines and move forward thru the search.

like image 583
Vibration Of Life Avatar asked Nov 13 '12 19:11

Vibration Of Life


People also ask

How do I grep a whole line in Linux?

The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. The output shows only the lines with the exact match.

How do you grep and show lines before and after?

To also show you the lines before your matches, you can add -B to your grep. The -B 4 tells grep to also show the 4 lines before the match. Alternatively, to show the log lines that match after the keyword, use the -A parameter. In this example, it will tell grep to also show the 2 lines after the match.

How do I search for a specific line in Linux?

With grep -i -n 'string' you can search for a particular string and find out what line it is on.


2 Answers

grep --after-context=5 --before-context=10 'Nov 12 2012' yourfile.log 

That'll show each line that contains your date text, as well as 10 lines of text BEFORE the line that matched, and 5 lines AFTER the line that matched.

like image 149
Marc B Avatar answered Oct 13 '22 00:10

Marc B


You can use grep to show matching lines and less as a pager:

grep 'Nov 12 2012' /path/to/logfile | less 

Type 'space' at the end of each page to advance to the next screen of results.

like image 33
larsks Avatar answered Oct 12 '22 22:10

larsks