Linux 搜索单词并显示整行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13367412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux search for word and show entire line
提问by Vibration Of Life
I have a very large log file (6 gig).
我有一个非常大的日志文件(6 演出)。
I want to search for 'Nov 12 2012' and print off each line.
我想搜索“2012 年 11 月 12 日”并打印出每一行。
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.
我是 linux 新手,不知道这是如何完成的。很可能需要更多选项来查看 X 行并继续搜索。
采纳答案by Marc B
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.
这将显示包含日期文本的每一行,以及匹配行之前的 10 行文本和匹配行之后的 5 行。
回答by larsks
You can use grep
to show matching lines and less
as a pager:
您可以使用grep
来显示匹配的行和less
作为寻呼机:
grep 'Nov 12 2012' /path/to/logfile | less
Type 'space' at the end of each page to advance to the next screen of results.
在每个页面的末尾键入“空格”以前进到下一个结果屏幕。
回答by sampson-chen
You can use grep
as follows:
您可以grep
按如下方式使用:
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.
grep
是用于搜索模式的命令/工具的名称'Nov 12 2012'
: 紧随其后grep
并以至少 1 个空格分隔,指定要搜索的模式file_to_search.log
: 作为grep
这里的最后一个参数,您指定要搜索的文件> search_results.log
:>
表示输出重定向。这里的意思是“将此命令的输出写入名为 的文件search_results.log
。如果该文件已经存在,则完全覆盖它。
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
,因此请使用以下任一方法:
less search_results.log
gedit search_results.log
emacs search_results.log
vim search_results.log
回答by Raju K
Open the file in a script format and then search with the keyword which you want to find as follows.
以脚本格式打开文件,然后使用您要查找的关键字进行搜索,如下所示。
$vi <logfilename>
&
:/search
回答by Arnon Rodman
This is a good way for locating errors at big log files : grep --after-context=5 --before-context=10 'Error' yourfile.log or grep --after-context=5 --before-context=10 'Exception' yourfile.log
这是在大日志文件中定位错误的好方法: grep --after-context=5 --before-context=10 'Error' yourfile.log 或 grep --after-context=5 --before-context=10 '例外' yourfile.log
回答by Mandrake
grep -A 5 -B 10 'Nov 12 2012' <yourfile.log>
-A <n>
to view n
lines after and -B <n>
for n
lines before.
Or, for both together -C <n>
i.e display n
lines before and after the searched text.
-A <n>
查看n
后面-B <n>
的n
行和前面的行。或者,将两者放在一起,-C <n>
即n
在搜索文本之前和之后显示行。