用于提取特定搜索字符串模式前后的行的Unix命令
如何在文件中搜索行并提取搜索行的行上方和行下方的行 我的意见是用于提取特定搜索字符串模式前后的行的Unix命令,unix,grep,Unix,Grep,如何在文件中搜索行并提取搜索行的行上方和行下方的行 我的意见是 Tue Jun 26 14:59:46 2012 Warning ffffffff act_msg_ctms_remove_from_pending_queue: deleting message 44817201 from the queue. Tue Jun 26 14:59:46 2012 Warning ffffffff Finishing processing record number 44817201 Tue Ju
Tue Jun 26 14:59:46 2012
Warning ffffffff act_msg_ctms_remove_from_pending_queue: deleting message 44817201 from the queue.
Tue Jun 26 14:59:46 2012
Warning ffffffff Finishing processing record number 44817201
Tue Jun 26 14:59:46 2012
Warning 5000000 activity_queue_manager_finish_cb: unknown activity 120.
Tue Jun 26 14:59:46 2012
Warning ffffffff Activity State Machine priority (2) finished
Tue Jun 26 14:59:46 2012
Warning ffffffff
====================================================
Processing database file "INCOMING_MESSAGES" record number 47810234 from user "(unknown)"
Tue Jun 26 14:59:46 2012
Warning ffffffff ACTIVITY data: rec_num (47810234) size (116)
Tue Jun 26 14:59:46 2012
Warning ffffffff activity status: ACT_SENT
Tue Jun 26 14:59:46 2012
Warning ffffffff MESSAGE body "MVT
QFA6673/26.VHQOS.BNE
EA0541
"
Tue Jun 26 14:59:46 2012
Warning ffffffff Finishing processing record number 47810234
Tue Jun 26 14:59:46 2012
Warning ffffffff Activity State Machine priority (1) finished
Tue Jun 26 14:59:46 2012
Warning ffffffff
End processing record number 47810234
====================================================
我要求我的输出像
/
Tue Jun 26 14:59:46 2012
Warning ffffffff MESSAGE body "MVT
QFA6673/26.VHQOS.BNE
EA0541"
/
我的搜索字符串将是MVT
请帮助Grep可以选择在比赛前后立即显示行。下面命令行中的数字是匹配前后要显示的适当行数。例如
grep-A3-B5 yoursearchpattern输入文件模式
man grep对于选项的详细信息非常有用
假设您有GNU grep,要检查是否可以使用--version选项:
> grep --version
GNU grep 2.6.3
比赛前后三行
grep -C 3 pattern filename
要更多地控制要为匹配显示的前后行数,请使用
grep -A (num of after) -B (num of lines before) pattern filename
从man grep
:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of matches.
-a, --text
Process a binary file as if it were text;
this is equivalent to the --binary-files=text option.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of matches.
-C NUM, --context=NUM
Print NUM lines of output context.
Places a line containing -- between contiguous groups of matches.
谢谢,+1为-C选项,一直在使用A和B,今天学习了一个新字母@jpe:但使用A和B是对问题的正确回答,因为他在“之后”和“之前”的上下文行不同。:)在这两种情况下,它都会抛出一个称为非法的错误option@wanted:这是给gnu grep的。你在用哪个grep?