回答
是,使用
grep -B num1 -A num2
到包括在比賽前上下文的NUM1線,並且在比賽後上下文NUM2線。
編輯:
似乎OP是使用AIX。這有一組不同的選項不包括-B和-A
this link描述了AIX 4.3的grep(不看好)
馬特的perl腳本可能是一個更好的解決方案。
它不能在我的systrm上運行 grep -B 3 -A 2 sunny sachin grep:非法選項-B B 用法:grep -hblcnsviw模式文件。 。 。 – 2009-11-06 06:05:35
我正在AIX上工作。 – 2009-11-06 06:13:04
我已經重申了這個問題。我希望AIX專家出現。 – pavium 2009-11-06 06:16:20
肯定有(從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.
它不適用於AIX,我的歉意還沒有提到,這個問題在AIX上。 – 2009-11-06 06:30:09
檢查我的其他答案,它應該適用於AIX,因爲AIX支持-n參數。 – Puppe 2009-11-06 06:36:36
我們可以爲這個 – 2009-11-06 06:41:45
從標籤,很可能系統有一個grep可能不支持提供上下文(Solaris是一個不支持而我不記得AIX的系統)。如果是這樣的話,有一個perl腳本可以幫助http://www.sun.com/bigadmin/jsp/descFile.jsp?url=descAll/cgrep__context_grep。
如果你的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線(也就是你匹配的行加上前行後的數量)
這是最接近GNU grep -A-B-C輸出的版本(除了不輸出組分隔符( - ))。 – Bruce 2017-11-24 11:21:46
您可以使用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
是的,我試過,但它不會工作,出現錯誤 awk:第1行附近的語法錯誤 awk:在第1行附近跳出 – 2009-11-17 05:28:17
如果有,請使用nawk。 – ghostdog74 2009-11-17 06:07:07
這是我平時做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.*
當我想要匹配更多像flow這樣的文件時,我應該如何重寫它。* – hudi 2015-08-25 08:16:56
您可以用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
爲了清晰起見,更新了上面的文章。 – 2015-08-25 17:25:24
- 1. 上面的Unix grep行和級聯
- 2. Unix grep命令
- 3. unix幫助grep?
- 4. UNIX grep命令
- 5. Unix grep查詢
- 6. UNIX grep命令(grep的-v grep的)
- 7. 高級GREP/AWK - 出口字符> [X]
- 8. Grep multiple「and」expressions UNIX
- 9. 的Unix grep命令
- 10. Unix環境下的高級編程
- 11. 高級的unix命令行技巧
- 12. 在unix中,與unix命令「history | grep keyword」
- 13. Unix bash切割和grep
- 14. UNIX命令:grep的-A
- 15. unix中使用grep命令
- 16. unix中的grep用戶名
- 17. grep和分裂在UNIX
- 18. Git Grep =>爲什麼`git grep -h`不能用於Unix管道?
- 19. 的Unix grep的正則表達式如何用grep的
- 20. Unix-如何grep裏面的PKZIP文件?
- 21. 在unix中用分號表示的grep
- 22. Unix grep命令輸出垃圾
- 23. grep的方括號,UNIX的bash
- 24. Unix:使用grep查找字符串-Rn
- 25. 用於Java日誌的Unix grep
- 26. bash grep mac地址unix/linux用分號
- 27. grep用unix中的cut命令?
- 28. 幫助與unix焦油和grep循環
- 29. Unix每小時grep多種模式
- 30. Unix的尾巴和grep等同於Windows
http://betterthangrep.com/ – Artelius 2009-11-06 05:57:48
Dup的:http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-行 – paxdiablo 2009-11-06 06:01:35
@Artelius,我不能依靠其他軟件。 – 2009-11-06 06:07:57