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

问题描述

我已阅读,但我没有使用XML,所以它与我的Linux命令有点不同。



我有以下数据:

   使用<$ 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命令字符串之间的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:07