提取两个相同定界符(多实例定界符)之间的行

我想要的是从第一次出现定界符到最后一次出现相同定界符,包括它们之间的所有内容,定界符可能在日志文件中多次出现。 

http://img2.mukewang.com/60794f560001e21604130776.jpg

sample.log


[T=iaaaaaaaaa134]:SampleClass9: 

[T=iaaaaaaaaa134]:SampleClass7: 

[T=iaaaaaaaaa134]:SampleClass3: 

[T=iaaaaaaaaa134]:SampleClass1: 

[T=i8732jddcd234]:SampleClass1: 

[T=i8732jddcd234]:SampleClass2: 

[T=i8732jddcd234]:SampleClass3: 

[T=i8732jddcd234]:SampleClass4: 

Exception:NullPointerException:

    sampte 1

    sampte 1

    sampte 1

    sampte 1

    sampte 1

    

[T=i8732jddcd234]:SampleClass00: 

[T=i8732jddcd234]:SampleClass00: 

[T=i8732jddcd234]:SampleClass00: 

[T=i8732jddcd234]:SampleClass00: 

[T=i8732jddcd234]:SampleClass00: 

Exception:NullPointerException2:

    sampte 2

    sampte 2

    sampte 2

    sampte 2

    sampte 2

[T=i8732jddcd234]:SampleClass12: 

[T=i8732jddcd234]:SampleClass32: 

[T=i8732jddcd234]:SampleClass22: 

[T=2eeeeeeeee234]:SampleClass32: 

[T=2eeeeeeeee234]:SampleClass82: 

[T=2eeeeeeeee234]:SampleClass22: 

[T=2eeeeeeeee234]:SampleClass22: 

例如:我想提取从第一次出现的i8732jddcd234(定界符)到最近一次出现的i8732jddcd234及其之间的所有行。可能使用awk,sed,grep linux命令。因为这是linux服务器上的日志文件。我尝试过


awk /'i8732jddcd234','i8732jddcd234'/ test.log

当然,这是行不通的

烙印99
浏览 272回答 3
3回答

蝴蝶刀刀

以下内容awk可能对您有所帮助。awk '/i8732jddcd234/{if(!first){first=FNR};end=FNR} {a[FNR]=$0} END{for(i=first;i<=end;i++){print a[i]}}'&nbsp; Input_file现在也添加非单一衬里形式的解决方案。awk '/i8732jddcd234/ {&nbsp; &nbsp;if (!first) {&nbsp; &nbsp; &nbsp; first = FNR&nbsp; &nbsp;}&nbsp; &nbsp;end = FNR}{&nbsp; &nbsp;a[FNR] = $0}END {&nbsp; for(i = first; i <= end; i++) {&nbsp; &nbsp; &nbsp;print a[i]&nbsp; }}'&nbsp; Input_file

慕莱坞森

last=$(nl sample.log | tac | awk '/i8732jddcd234/ {print $1; exit}')sed -n "/i8732jddcd234/,${last}p" sample.log或者,用awk进行2次遍历文件:awk -v code=i8732jddcd234 '&nbsp; &nbsp; NR == FNR {&nbsp; &nbsp; &nbsp; &nbsp; if ($0 ~ code) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!first) first=FNR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last=FNR&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; next&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; first <= FNR && FNR <= last' sample.log sample.log

翻过高山走不出你

这可能对您有用(GNU sed):sed&nbsp;'/i8732jddcd234/!d;:a;n;:b;//ba;$d;N;bb'&nbsp;file删除所有行,直到包含的行i8732jddcd234。打印包含的行i8732jddcd234,然后检查下一行是否也行,是否重复。否则,请收集行,直到下次出现i8732jddcd234或文件结束为止,在这种情况下,可以删除累积的行。
打开App,查看更多内容
随时随地看视频慕课网APP