0

move <acd> to new line and values after </acd> to new line

Input File:

abcd<acd>aghjdkadlkh</acd> <bud> dghakj </bud>

expected Output:

abcd
<acd>
aghjdkadlkh
</acd>
<bud>dghakj</bud>
4
  • add an explanation about what you are trying to do... and what did you try to solve it? Commented Jun 13, 2017 at 2:13
  • am trying to edit the file as move to a new line once the first matching pattern occurs and move the lines below the second matching pattern to new lines Commented Jun 13, 2017 at 3:16
  • Where have the spaces in the last line vanished? Is it intended or a typo? Commented Jun 13, 2017 at 6:54
  • Its a typo on the spaces in last line. Commented Jun 13, 2017 at 15:04

1 Answer 1

1

sed with ERE (-E):

sed -E 's#</?acd>#\n&\n#g'
  • s#</?acd>#\n&\n#g substitutes <acd> and </acd> with newlines before and after the pattern

After the operation, to get rid of the leading space of <bud> dghakj </bud> removed too, tack a tiny sed:

sed -E 's#</?acd>#\n&\n#g' ... | sed 's/^ //'

Example:

% sed -E 's#</?acd>#\n&\n#g' <<<'abcd<acd>aghjdkadlkh</acd> <bud> dghakj </bud>' 
abcd
<acd>
aghjdkadlkh
</acd>
 <bud> dghakj </bud>

% sed -E 's#</?acd>#\n&\n#g' <<<'abcd<acd>aghjdkadlkh</acd> <bud> dghakj </bud>' | sed 's/^ //'
abcd
<acd>
aghjdkadlkh
</acd>
<bud> dghakj </bud>
2
  • sed -E 's#</?acd>#\n&\n#g; s#\n[[:blank:]]+#\n#g' would do it and leave any unintended leading spaces untouched as well. Commented Jun 13, 2017 at 6:53
  • I tried this code for my xml file and also the above example, it does not make any changes to the input. Commented Jun 13, 2017 at 15:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.