我有一个日志文件trace.log.在这里面,我需要到grep为包含在字符串中的内容<tag>和</tag>.这对字符串有多组,我只需要在最后一组(换句话说,来自tail日志文件)之间返回内容.
额外信用:只有当内容包含"testString"时,我才能返回两个字符串中包含的内容?
谢谢你的期待.
编辑:搜索参数和包含在不同的行上,大约有100行内容将它们分开.内容就是我追求的......
fed*_*qui 34
用于tac以相反的方式打印文件,然后grep -m1只打印一个结果.外观背后并展望检查文本之间<tag>和</tag>.
tac a | grep -m1 -oP '(?<=tag>).*(?=</tag>)'
Run Code Online (Sandbox Code Playgroud)
鉴于此文件
$ cat a
<tag> and </tag>
aaa <tag> and <b> other things </tag>
adsaad <tag>and last one</tag>
$ tac a | grep -m1 -oP '(?<=tag>).*(?=</tag>)'
and last one
Run Code Online (Sandbox Code Playgroud)
编辑:搜索参数和包含在不同的行上,大约有100行内容将它们分开.内容就是我追求的......
然后它有点棘手:
tac file | awk '/<\/tag>/ {p=1; split($0, a, "</tag>"); $0=a[1]};
/<tag>/ {p=0; split($0, a, "<tag>"); $0=a[2]; print; exit};
p' | tac
Run Code Online (Sandbox Code Playgroud)
想法是反转文件并使用标志p来检查是否<tag>已经出现.它会在</tag>出现时开始打印并在<tag>到达时完成(因为我们正在以相反的方式阅读).
split($0, a, "</tag>"); $0=a[1]; 之前获取数据 </tag>split($0, a, "<tag>" ); $0=a[2]; 之后获取数据 <tag>给定这样的文件a:
<tag> and </tag>
aaa <tag> and <b> other thing
come here
and here </tag>
some text<tag>tag is starting here
blabla
and ends here</tag>
Run Code Online (Sandbox Code Playgroud)
输出将是:
$ tac a | awk '/<\/tag>/ {p=1; split($0, a, "</tag>"); $0=a[1]}; /<tag>/ {p=0; split($0, a, "<tag>"); $0=a[2]; print; exit}; p' | tac
tag is starting here
blabla
and ends here
Run Code Online (Sandbox Code Playgroud)
Sla*_*get 23
如果像我一样,你无法访问tac,因为你的系统管理员不会打球,你可以尝试:
grep pattern file | tail -1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43517 次 |
| 最近记录: |