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>
  • add an explanation about what you are trying to do... and what did you try to solve it? – Sundeep Jun 13 '17 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 – Parthi Jun 13 '17 at 3:16
  • Where have the spaces in the last line vanished? Is it intended or a typo? – user218374 Jun 13 '17 at 6:54
  • Its a typo on the spaces in last line. – Parthi Jun 13 '17 at 15:04
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>
  • sed -E 's#</?acd>#\n&\n#g; s#\n[[:blank:]]+#\n#g' would do it and leave any unintended leading spaces untouched as well. – user218374 Jun 13 '17 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. – Parthi Jun 13 '17 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.