用于在特定搜索字符串模式之后和之后提取行的Unix命令

wan*_*ted 3 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 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
Run Code Online (Sandbox Code Playgroud)

================================================== ==

我要求我的输出就像

/

Tue Jun 26 14:59:46 2012
 Warning ffffffff MESSAGE body "MVT
QFA6673/26.VHQOS.BNE
EA0541"

/
Run Code Online (Sandbox Code Playgroud)

我的搜索字符串是MVT.

请帮忙

pyf*_*unc 5

比赛前后三条线

grep -C 3 pattern filename 
Run Code Online (Sandbox Code Playgroud)

要更好地控制要为匹配显示的后续行数和前一行数,请使用

grep -A (num of after) -B (num of lines before)  pattern filename
Run Code Online (Sandbox Code Playgroud)

来自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.
Run Code Online (Sandbox Code Playgroud)