Paul Bergström
Paul Bergström

Reputation: 253

Adding new lines in XML file

I have a bunch of XML-files that all start with the XML-declaration

<?xml version="1.0" encoding="UTF-8"?>

After the final ">" it just goes on directly with the start of the following tag - like this: <?xml version="1.0" encoding="UTF-8"?><tag>

I would like to split it there to make a new line followed by another new line. Like this:

<?xml version="1.0" encoding="UTF-8"?>
New line
New line
<tag>

How could this be done?

Many thanks in advance:-)

/Paul

Upvotes: 0

Views: 904

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

based on your definition (start with) and content

sed '1 {
   /<[?]xml/ {
      s/<tag>//
      a \
new line 1 (whatever also just a \n like "new line 2") \
\
<tag>
      }
   }' YourFile
  • Adapt the line using a append with free text (new line are \ char) with a bit of security (take only 1st line and starting with ?xml)
  • use -i for inline modification on GNU sed

Upvotes: 0

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

With sed:

AMD$ sed 's/<?xml version="1.0" encoding="UTF-8"?>/&\n\n\n/' File
<?xml version="1.0" encoding="UTF-8"?>


<tag>

Just replace the pattern <?xml version="1.0" encoding="UTF-8"?> with the same pattern followed by 3 newlines (this will create 2 newlines before <tag>.

Use sed -i.bak 's/<?xml version="1.0" encoding="UTF-8"?>/&\n\n\n/' File for inplace substitution.

Upvotes: 2

Related Questions