0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked like a charm. Huge thanks. I wrapped it in a for loop then I was done.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.