2017-08-10 63 views
0

這裏我正在練習grep命令.Am不清楚以下grep條件是如何工作的?Grep條件匹配如何工作?

a_file:

boot 
record 
boots 
process 
broken 
commands 

我曾嘗試下面的命令: -

1. grep -A0 "boo" a_file 

result: 
boot 
-- 
boots 

2.grep -A1 "boo" a_file 
result: 
boot 
record 
boots 
process 

3.grep -A2 "boo" a_file 
result: 
boot 
record 
boots 
process 
broken 

4.grep -A3 "boo" a_file 
result: 
boot 
record 
boots 
process 
broken 
commands 

Note:I had studied this grep command from terminal man grep. 

我的查詢:

1.What is the purpose of switch -A? 
2.How the context lines are ordered for every numeric values (i.e 1,2,3)? 
+0

你試圖理解你的第二個問題? 'grep -A'如何執行它的工作? – CWLiu

+0

A表示匹配上下文之後。如果我給出A2,它將查找搜索字符串(即boo)。在由於A2匹配的行之後,它將在匹配字符串後面打印下兩行。這就是我從命令中理解的內容。 @CWLiu – PathFinder

+0

是的,你的理解基本上是正確的,你的第二個問題是什麼,詳述它。這將幫助這裏的人爲您提供更準確的答案。 – CWLiu

回答

0

關於第一個問題,-A-B是真正有用的,而你會發現一個漫長而複雜的日誌。您可以使用-A-B選項查看日誌中的更多細節,並在日誌中搜索到的模式之前加快調試效率。

對於第二個問題,從srcgrep,對選項-A沒有特別的限制。它的論點,即out_after,在src中定義爲long int

static intmax_t out_after; /* Lines of trailing context. */ 

我認爲要打印的尾行要等到EOF或下一個匹配的圖案被打(尾行的計數纔會被重置)。