Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search for a pattern spanning multiple lines between 2 strings and does not include a different string, then replace with new value

The title may be a bit vague, but I have a file that contains something like this

<Tag>
 <Tag 
    111
    222
 />
</Tag>

Using bash script (grep, sed, awk, etc.) I want to search for the inside tag and replace it with some new content

 <Tag>
     <Tag 
        333
        444
     />
    </Tag>

The new content can be all in one line, ie <Tag 333 444 />
I’m struggling with how to search for the inside Tag only, I can’t find a way to extract a string starting with <Tag and end with /> but doesn’t contain >. Any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If this, using GNU sed for -E and -z, isn’t all you need then edit your question to provide more truly representative sample input/output including cases you have that this doesn’t work for:

$ sed -Ez 's:<Tag[^<>]+/>:<Tag 333 444 />:' file
<Tag>
 <Tag 333 444 />
</Tag>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading