InsideDarkWeb.com

How to insert a newline character after each occurrence of a specific XML tag in a file?

How to insert a newline character after each occurrence of a specific XML tag in a file?

My file has multiple tags </Data> and I want to insert a NewLine character after each </Data> tag

I’ve been trying various combinations with sed but it never inserts the newline character 🙁

The latest was sed '/</Data>/ a n'

Thanks!

Unix & Linux Asked by Megalex on November 11, 2021

5 Answers

5 Answers

Tried in python

cat inputfile

san</Data>abhi</Data>san

Python

#!/usr/bin/python
g="</Data>"
import re
k=open('k.txt','r')
for i in k:
    m="{0}".format(g+"n")
    q=re.sub(g,"{0}",i).format(m)
    print q

output

san</Data>
abhi</Data>
san

Answered by Praveen Kumar BS on November 11, 2021

sed 's:</Data>:&
:g'

Or when calling sed from bash or other shell that supports $'<char>' to expand an escape sequence:

sed 's:</Data>:&'$'n'':g'

Or with GNU sed:

sed 's:</Data>:&n:g'

e.g.:

$ printf 'foo</Data>bar</Data>etcn'
foo</Data>bar</Data>etc
$
$ printf 'foo</Data>bar</Data>etcn' | sed 's:</Data>:&n:g'
foo</Data>
bar</Data>
etc

Answered by Ed Morton on November 11, 2021

Instead of just formatting the Data tag, you can format the whole XML file with newlines after each XML nodes. This is the proper solution.

Using is not elegant and not universal nor a proper solution.

Think of a CDATA section from within your XML with:

<![CDATA[foobar</Data>qux]]>

Instead, using an XML parser like xmlstarlet:

xmlstarlet format -R file.xml

Example:

cat file.xml
<foo>
<a>jkj</a><data>fghj</data><x>w</x>
</foo>

$ xmlstarlet fo -R file
<?xml version="1.0"?>
<foo>
  <a>jkj</a>
  <data>fghj</data>
  <x>w</x>
</foo>

Answered by Gilles Quenot on November 11, 2021

sed '/</Data>/ a \n' will insert the newline, however, if the string exists more than once in a line (typical xml may be one long line) it simply appends a newline after it instead of after every occurrence.

If you really want a newline after every occurrence, sed 's/(</Data>)/1n/g' should do the trick.

Answered by strobelight on November 11, 2021

This might not be the most elegant but it works:

sed 's/(</Data>)/1n/ig'

Answered by Artem S. Tashkinov on November 11, 2021

Add your own answers!

Related Questions

How to adjust NVIDIA GPU fan speed on a headless node?

2  Asked on December 16, 2020 by aleksandr-dubinsky

       

Linux VM unresponsive

0  Asked on December 16, 2020 by ykaly

 

Upgrading Berkeley DB rpm in a Centos OS

1  Asked on December 16, 2020 by gr68

         

How to redirect all network traffic through Tor?

0  Asked on December 15, 2020 by ananthakrishna-k

     

Feed script variable to command that prompts for user input

1  Asked on December 15, 2020 by vishnu-sajeevan

   

Configuring SNMP Traps

1  Asked on December 15, 2020 by kubomd

   

Wrong notification system in KDE Plasma

1  Asked on December 14, 2020 by arthur-m

       

start chromium on startup using systemd

1  Asked on December 14, 2020

     

DRBD Failure: (127) Device minor not allocated

1  Asked on December 14, 2020 by tanatos-daniel

       

Using bash shell function inside AWK

7  Asked on December 13, 2020 by tiny

   

Chrome on Gnome randomly fails to go full screen

1  Asked on December 13, 2020 by mikev

       

.vnc folder becoming too large

0  Asked on December 13, 2020 by hardcorehenry

 

TTY size with dual monitors

0  Asked on December 12, 2020 by user66554

     

run a script in multiple folders in parallel

5  Asked on December 11, 2020 by user233520

       

Fill missing fields with values from the line below

4  Asked on December 10, 2020 by joni

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.