1

I would like to replace the current element inside example path tag from:

...
<examples>
<example path='/test/test123/test12345.txt'/>
<examples>
...

to

...
<examples>
<example path=/test/test123/>
<examples>
...

with

sed -i 's/<example path=\/test\/test123\/test12345.txt'>/<example path=\/test\/test123\/>' 123.xml

but it says:

-bash: example: No such file or directory

2
  • You didnot set a "sed command". To correct it you have to write: sed -i s/'escaped-string'/'target-string'/ filename.xml
    – schweik
    Commented Sep 18, 2020 at 8:54
  • Your target <example path=/test/test123/> is either wrong (it is) or ambiguous. Either it's an unclosed element <example path="/test/test123/"> with no closing <element/> shown, or it's a singleton <example path="/test/test123"/>. This is why quoting values is necessary Commented Sep 21, 2020 at 11:31

3 Answers 3

2

You get the error because of mismatched single quotes.

The quoted string ends at the second quote. The shell finds unquoted >/<example and interprets this as redirection.

Additionally your sed command is incomplete. The trailing separator / is missing.

I also fixed the sed commands to match the input data which has been edited.

If you want to have single quotes inside the sed command you have to either use double quotes around the command and make sure there is nothing inside the double quotes that would get expanded by the shell

sed -i "s/<example path='\/test\/test123\/test12345.txt'\/>/<example path=\/test\/test123\/>/" 123.xml

or use escaped single quotes like this

sed -i 's/<example path='\''\/test\/test123\/test12345.txt'\''\/>/<example path=\/test\/test123\/>/' 123.xml

BTW: If you use a different separator character (not slash /) you don't need to escape the slashes in the pattern or replacement, e.g.

sed -i "s#<example path='/test/test123/test12345.txt'/>#<example path=/test/test123/>#" 123.xml
sed -i 's#<example path='\''/test/test123/test12345.txt'\''/>#<example path=/test/test123/>#' 123.xml

Your desired output as shown in the question is not valid XML. Attributes must be quoted.

0
2

You should use an XML editor to edit XML. It can handle changes to the layout of the XML schema within the file, unexpected comments, etc. My preferred one is xmlstarlet

Example file, x.xml

<root>
  <examples/>
  <example path='/test/test123/test12345.txt'/>
  <examples/>
</root>

Command, which updates the attribute path in any element example to the value /test/test123

xmlstarlet ed --update '//example/@path' --value '/test/test123' x.xml

Output

<?xml version="1.0"?>
<root>
  <examples/>
  <example path="/test/test123"/>
  <examples/>
</root>
0
0

sed -i -e s/test\///g 123.xml to just delete, sed -i -e s/test\//replacement/g 123.xml to replace with something

4
  • 1
    While that answer describes the general usage of sed, it does not deal with the particular challenges of editing structured text (like XML or JSON) which is the key problem here.
    – AdminBee
    Commented Sep 18, 2020 at 11:30
  • Also, it is recommeded to always quote your sed expressions to prevent unwanted behaviour (as we have seen in the OPs problem).
    – AdminBee
    Commented Sep 18, 2020 at 11:37
  • @AdminBee, there is no modification of the xml structure needed, but the content.
    – Olaf
    Commented Sep 23, 2020 at 4:31
  • That is not quite the point. Structured text is characterized by having to locate the content to be being modified within possible nested layers of start/end tags, many of which look similar in pattern and only differ by additional attributes that need to be taken into account. They are also usually spread out over several lines and are not easily accessible by the "single-line" search you describe - consider the case that the test/test123 path does not only occur in <examples><example path= ../.></example>, but also in <config><cfgfile path= .../></config> where no change should occur.
    – AdminBee
    Commented Sep 23, 2020 at 6:57

You must log in to answer this question.

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