2009-11-06 46 views
4

通常使用grep命令來顯示包含指定模式的行。有什麼方法可以在包含指定模式的行之前和之後顯示n行?高級grep unix

這可以通過awk來實現嗎?

+0

http://betterthangrep.com/ – Artelius 2009-11-06 05:57:48

+1

Dup的:http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-行 – paxdiablo 2009-11-06 06:01:35

+0

@Artelius,我不能依靠其他軟件。 – 2009-11-06 06:07:57

回答

7

是,使用

grep -B num1 -A num2 

到包括在比賽前上下文的NUM1線,並且在比賽後上下文NUM2線。

編輯:

似乎OP是使用AIX。這有一組不同的選項不包括-B和-A

this link描述了AIX 4.3的grep(不看好

馬特的perl腳本可能是一個更好的解決方案。

+0

它不能在我的systrm上運行 grep -B 3 -A 2 sunny sachin grep:非法選項-B B 用法:grep -hblcnsviw模式文件。 。 。 – 2009-11-06 06:05:35

+0

我正在AIX上工作。 – 2009-11-06 06:13:04

+0

我已經重申了這個問題。我希望AIX專家出現。 – pavium 2009-11-06 06:16:20

0

肯定有(從grep的手冊頁):

-B NUM, --before-context=NUM 
      Print NUM lines of leading context before matching lines. 
      Places a line containing a group separator (--) between 
      contiguous groups of matches. With the -o or --only-matching 
      option, this has no effect and a warning is given. 

    -A NUM, --after-context=NUM 
      Print NUM lines of trailing context after matching lines. 
      Places a line containing a group separator (--) between 
      contiguous groups of matches. With the -o or --only-matching 
      option, this has no effect and a warning is given. 

,如果你想之前和之後的比賽中,用線條相同數量:

-C NUM, -NUM, --context=NUM 
      Print NUM lines of output context. Places a line containing a 
      group separator (--) between contiguous groups of matches. With 
      the -o or --only-matching option, this has no effect and a 
      warning is given. 
+0

它不適用於AIX,我的歉意還沒有提到,這個問題在AIX上。 – 2009-11-06 06:30:09

+0

檢查我的其他答案,它應該適用於AIX,因爲AIX支持-n參數。 – Puppe 2009-11-06 06:36:36

+0

我們可以爲這個 – 2009-11-06 06:41:45

1

如果你的sed你可以使用這個shell腳本

BEFORE=2 
AFTER=3 
FILE=file.txt 
PATTERN=pattern 
for i in $(grep -n $PATTERN $FILE | sed -e 's/\:.*//') 
    do head -n $(($AFTER+$i)) $FILE | tail -n $(($AFTER+$BEFORE+1)) 
done 

它所做的是,grep的-n前綴每場比賽有它被發現,sed的帶所有,但該行發現線路在。然後,您可以使用head獲取線路,直到找到線路爲止,再加上$ AFTER線路。這是再通過管道輸送到尾只得到$ BEFORE + $ AFTER + 1線(也就是你匹配的行加上前行後的數量)

+0

這是最接近GNU grep -A-B-C輸出的版本(除了不輸出組分隔符( - ))。 – Bruce 2017-11-24 11:21:46

0

您可以使用AWK

awk 'BEGIN{t=4} 
c--&&c>=0 
/pattern/{ c=t; for(i=NR;i<NR+t;i++)print a[i%t] } 
{ a[NR%t]=$0} 
' file 

輸出

$ more file 
1 
2 
3 
4 
5 
pattern 
6 
7 
8 
9 
10 
11 

$ ./shell.sh 
2 
3 
4 
5 
6 
7 
8 
9 
+0

是的,我試過,但它不會工作,出現錯誤 awk:第1行附近的語法錯誤 awk:在第1行附近跳出 – 2009-11-17 05:28:17

+0

如果有,請使用nawk。 – ghostdog74 2009-11-17 06:07:07

3

這是我平時做AIX:

before=2 << The number of lines to be shown Before >> 
after=2 << The number of lines to be shown After >> 
grep -n <pattern> <filename> | cut -d':' -f1 | xargs -n1 -I % awk "NR<=%+$after && NR>=%-$before" <filename> 

如果你不想額外的2 varialble就是你可以隨時使用它的一個行:

grep -n <pattern> <filename> | cut -d':' -f1 | xargs -n1 -I % awk 'NR<=%+<<after>> && NR>=%-<<before>>' <filename> 

假設我有一個模式「堆」和文件名是flow.txt 我想前2行和後3行。該命令將是這樣的:

grep -n 'stack' flow.txt | cut -d':' -f1 | xargs -n1 -I % awk 'NR<=%+3 && NR>=%-2' flow.txt 

我之前,只需要2線 - 該命令將是這樣的:

grep -n 'stack' flow.txt | cut -d':' -f1 | xargs -n1 -I % awk 'NR<=% && NR>=%-2' flow.txt 

我想3線後,僅 - 該命令將會怎樣:

grep -n 'stack' flow.txt | cut -d':' -f1 | xargs -n1 -I % awk 'NR<=%+3 && NR>=%' flow.txt 

多個文件 - 將其更改爲Awk & grep。從上面的模式'堆'與文件名是流。* - 前2行和後3行。該命令會像:

awk 'BEGIN { 
    before=1; after=3; pattern="stack"; 
    i=0; hold[before]=""; afterprints=0} 
    { 
    #Print the lines from the previous Match 
    if (afterprints > 0) 
     { 
     print FILENAME ":" FNR ":" $0 
     afterprints-- #keep a track of the lines to print after - this can be reset if a match is found 
     if (afterprints == 0) print "---" 
     } 
    #Look for the pattern in current line 
    if (match($0, pattern) > 0) 
     { 
     # print the lines in the hold round robin buffer from the current line to line-1 
     # if (before >0) => user wants lines before avoid divide by 0 in % 
     # and afterprints => 0 - we have not printed the line already 
     for(j=i; j < i+before && before > 0 && afterprints == 0 ; j++) 
     print hold[j%before] 
     if (afterprints == 0) # print the line if we have not printed the line already 
     print FILENAME ":" FNR ":" $0 
     afterprints=after 
     } 
    if (before > 0) # Store the lines in the round robin hold buffer 
     { hold[i]=FILENAME ":" FNR ":" $0 
     i=(i+1)%before } 
    }' flow.* 
+0

當我想要匹配更多像flow這樣的文件時,我應該如何重寫它。* – hudi 2015-08-25 08:16:56

+0

您可以用flow。*替換flow.txt,其中2個位置用於grep,一個用於awk。例如 - 對於模式「堆棧」,文件名是流。* - 前2行和後3行。該命令將如下所示:'grep -n'stack'flow。* | cut -d':'-f1 | xargs -n1 -I%awk'NR <=%+3 && NR> =% - 2'flow。*' – 2015-08-25 17:19:37

+0

爲了清晰起見,更新了上面的文章。 – 2015-08-25 17:25:24