0

I am trying to substitute name=src. with name=Web/ in my xml file using the sed command:

sed -i 's/name=src\./name=WebUi\//g' coverage.xml

but it gives an error.

Can anyone please provide an idea on the sed command?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ankit Kadali
  • 105
  • 1
  • 6
  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Feb 27 '19 at 21:39
  • wrt `it gives an error` - what error? When you take your car to a mechanic to be repaired you probably don't just drop it of and say `it gives an error`, I'd hope you tell her the error message/display icon/other symptoms. That's the best way to get help here too. Since your script is fine the problems either a missing required backup file name for your sed version or you have an issue elsewhere in your code around the call to sed. – Ed Morton Feb 28 '19 at 05:01

2 Answers2

0

Use:

sed 's/name=src/name=Web/g' filename.txt
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

When / appears as part of the text, whether in the regex or replacement, use a character other than / in the s/// notation (I chose |, but you can use any character that doesn't appear in the regex or replacement; a control character such as Control-A can often be effective and safe):

sed command sed -i 's|name=src\.|name=WebUi/|g' coverage.xml

Note that the -i option written like that only works with GNU sed; with BSD or macOS sed, you'd need -i '' instead. Using -i.bak works the same with both but leaves a backup file that should be removed.

It always worries me when I see people with broken sed scripts doing an in-place alter. You shouldn't think of using the -i option until you're confident the script works.


On review, I'm surprised that the original 's/name=src\./name=WebUi\//g' didn't work — the backslash before the slash should have been correct.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I expect it's the missing backup file name that's the problem, nothing to do with the actual script since it's fine as written. If the OP had posted the error message that might have been immediately obvious. – Ed Morton Feb 28 '19 at 05:03
  • 1
    @EdMorton: I suspect so too. I didn't hear back from the OP about which system he was using. At one point there was a comment about "`c` not followed by backslash" as the error, which would be consistent with BSD/macOS `sed` treating the script as the backup suffix and the file name (`coverage.xml`) as the 'script' with a `c` not followed by a backslash. – Jonathan Leffler Feb 28 '19 at 05:06