加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 为两个字符串之间的字符串最后一次出现的日志文件

发布时间:2020-12-15 19:33:44 所属栏目:安全 来源:网络整理
导读:我有一个日志文件trace.log。在其中,我需要grep包含在字符串 tag和 / taggt ;.这对字符串有多组,我只需要在最后一个集合之间返回内容(换句话说,从日志文件的尾部)。 额外的信用:只要内容包含“testString”,任何方式我可以返回包含在两个字符串内的内容
我有一个日志文件trace.log。在其中,我需要grep包含在字符串< tag>和< / tag&gt ;.这对字符串有多组,我只需要在最后一个集合之间返回内容(换句话说,从日志文件的尾部)。 额外的信用:只要内容包含“testString”,任何方式我可以返回包含在两个字符串内的内容? 谢谢你的看 编辑:搜索参数,并包含在不同的行上,大约100行内容分隔。内容是我以后…
使用tac打印文件,然后grep -m1打印一个结果。后面的外观和前瞻性检查< tag>之间的文本。和< / tag&gt ;.
tac a | grep -m1 -oP '(?<=tag>).*(?=</tag>)'

测试

给定这个文件

$ 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

更新

EDIT: The search parameters and are contained on different lines with
about 100 lines of content separating them. The content is what I’m
after…

那么这有点更棘手:

tac file | awk '/</tag>/ {p=1; split($0,a,"</tag>"); $0=a[1]};
                /<tag>/   {p=0; split($0,"<tag>");  $0=a[2]; print; exit};
                p' | tac

这个想法是反转文件并使用标志p来检查< tag>已经出现了还是没有。 < / tag>将开始打印< tag>出现并完成来了(因为我们正在读书)。

> split($ 0,a,“< / tag>”); $ 0 = A [1];在< / tag>之前获取数据
>分裂($ 0,一 “<标记>” 中); $ 0 = A [2];标签>在&LT获取数据;

测试

给定一个文件是这样的:

<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>

输出将是:

$ tac a | awk '/</tag>/ {p=1; split($0,"</tag>"); $0=a[1]}; /<tag>/ {p=0; split($0,"<tag>"); $0=a[2]; print; exit}; p' | tac
tag is starting here
blabla
and ends here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读