4

I have a structure of a project in my git repository and would like to run a shell command in git bash to replace all the > with >\n (LF). I tried the following command:

find . -name '*.xml' -exec sh -c 'sed 's/>/>\n/g "$0"' '{}' ';'

meaning to replace the above sets (> with >\n) in all xml files found in my project.

I don't know what I am doing wrong since I land in the editor with cursor ">"... (just making it pretty in an editor - e.g. Note++ is easy with plugins for XMLTools but I want my files altered to have in them the LF after each XML tag)

When I write the command for a file only - it works nice:

$ sed 's/>/>\n/g' MyFile.xml
10
  • 2
    Did you try running xmlstarlet fo MyFile.xml?
    – Kusalananda
    Jan 9, 2020 at 12:58
  • 1
    Teo, you've an odd number of single quotes in that line so it can't run. Please double-check what you've shown us. If this is the issue and you've been getting errors when you try to run it, please tell us!
    – roaima
    Jan 9, 2020 at 13:14
  • @roaima:I tried to simplify my command: find . -name '*.xml' -print0 | xargs -0 -I '{}' sed 's/>/>\n/g ' '{}' It worked on the Git Bash but nothing happend in the files...
    – Teo
    Jan 9, 2020 at 17:02
  • Of course not. You didn't ask that ↑ to change anything. But your question as posted has a syntax error on the find ... line. Please address that or make it clear that the reason it's not working for you is that you're getting an error that you can't fix.
    – roaima
    Jan 9, 2020 at 17:07
  • 1
    Oh I see. No, that's the secondary prompt from your shell. In this instance it's because you have an odd number of quoting characters in your command, so the shell needs the closing quote to complete your command.
    – roaima
    Jan 15, 2020 at 23:09

1 Answer 1

10

It's a bad idea to use sed for this, what you seems to do is to format the XML file.

It's a simple task for any XML parser, ex :

xmllint --format file.xml

This utility is often installed by default on GNU Linux, or with the package libxml2

or

xmlstarlet format file.xml
12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .