匹配Linux Grep命令字符串之间的任何内容 [英] Match Anything In Between Strings For Linux Grep Command

查看:1505
本文介绍了匹配Linux Grep命令字符串之间的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 grep包括换行符在内的所有字符,但我没有使用XML,所以它与我的Linux命令有点不同。



我有以下数据:

 示例行0< / span> 
< tag>示例行1< / tag>
< span>示例行1.5< / span>
< tag>
示例行2
< / tag>
示例行3
< span>示例行4< / span>

使用此命令 cat file.txt | grep -o'< tag。* tag> \ | ^ - 。*'我得到:

 < tag>示例行1< / tag> 

但是,我希望输出为:

 < tag>示例行1< / tag> 
< tag>示例行2< / tag>

如何匹配字符串之间的任何内容,包括换行符?



注意:我需要使用< tag 标签> 作为字符串,因为其他文件可以在行之间包含多个标记和文本。将更新示例数据以表明这一点。 使用<$ c 使用 gnu-awk $ c $< / tag> 作为记录分隔符:

  awk -v RS ='< ; / tag>''RT {gsub(/ \ n /,);打印$ 0 RT}'文件

< tag>示例行1< / tag>
< tag>示例行2< / tag>


I have read the post grep all characters including newline but I not working with XML so it's a bit different with my Linux command.

I have the following data:

Example line 0</span>
<tag>Example line 1</tag>
<span>Example line 1.5</span>
<tag>
Example line 2
</tag>
Example line 3
<span>Example line 4</span>

Using this command cat file.txt | grep -o '<tag.*tag>\|^--.*' I get:

<tag>Example line 1</tag>

However, I want the output to be:

<tag>Example line 1</tag>
<tag>Example line 2</tag>

How can I match anything between the strings, including the newline?

Note: I need to used <tag and tag> as strings because other files can contain multiple tags and text in between the lines. Will update sample data to show that.

解决方案

This is easier done with gnu-awk using </tag> as record separator:

awk -v RS='</tag>' 'RT {gsub(/\n/, ""); print $0 RT}' file

<tag>Example line 1</tag>
<tag>Example line 2</tag>

这篇关于匹配Linux Grep命令字符串之间的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆