-1

I want to just replace few strings in file with nothing, but sed replaces the whole line. Can someone help me with this?

line in file.xml:

<tag>sample text1 text2</tag>

My code:

 sed "s/'text1 text2'//" file.xml 2>/dev/null || :

I also tried

 sed -i -e "s/'text1 text2'//" file.xml 2>/dev/null || :

expected result:

<tag>sample</tag>

Actual result:

The whole line is removed from file.

Others:

text1 and text 2 are complex text with .=- characters in it

What can I do to fix this?

TIA

user1164061
  • 4,222
  • 13
  • 48
  • 74
  • 3
    That regex doesn't match the line you've shown us, so there must be something else going on. Please post a [mcve]. – melpomene Jun 10 '18 at 19:33
  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jun 11 '18 at 01:04

2 Answers2

1

Remove the single quotes:

sed "s/text1 text2//" file.xml
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Leonard
  • 13,269
  • 9
  • 45
  • 72
  • It still removes the line – user1164061 Jun 10 '18 at 21:20
  • Well that's puzzling. It works for me. $ sed "s/text1 text2//" txt.xml ----> sample – Leonard Jun 10 '18 at 21:24
  • Maybe cos I have an example which does not match reality! what if text1 and text 2 have characters - & = in them? e.g text1 is actually -text=1 and text2 is actually -text=2? So I need to replace -text=1 -text=2 with nothing – user1164061 Jun 10 '18 at 22:15
1

You could use

sed 's/\([^ ]*\)[^<]*\(.*\)/\1\2/' filename

Output:

<tag>sample</tag>

Grouping is used. First all characters till a space are grouped together, then all characters till a < are matched and all following characters are grouped into another group.

J...S
  • 5,079
  • 1
  • 20
  • 35