Linux search for word and show entire line
Open the file in a script format and then search with the keyword which you want to find as follows.
$vi <logfilename>
&
:/search
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.
You can use grep
as follows:
grep 'Nov 12 2012' file_to_search.log > search_results.log
Some explanations:
grep
is the name of the command / tool used for searching patterns'Nov 12 2012'
: immediately aftergrep
and separated by at least 1 space, you specify the pattern you want to search forfile_to_search.log
: as the last argument togrep
here, you specify the file(s) you want to search for> search_results.log
: The>
means output redirection. Here it means "write the output from this command to a file calledsearch_results.log
. If the file exists already, overwrite it completely.
After getting the output, you can view the results with a text editor of your choice, or with less
, so use any of the following:
less search_results.log
gedit search_results.log
emacs search_results.log
vim search_results.log
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.