Home > Enterprise >  Find and Replace SED
Find and Replace SED

Time:10-03

I am trying find a string in a file and remove from the file inbash script. Using sed find and replace the text but due to forward slash in the string the command is erroring out. Command :

sed -i 's/<host1>Sometext</host1>//g' test.xml

Input 
<test>
   <host1>Sometext</host1>
   <host2>Sometext</host2>
</test>

Output
<test>
</test>

Can someone please assist?

CodePudding user response:

1st solution: With sed try following code. Written and tested in GNU sed and your sed should support \n in it. Written and tested with your shown samples only.

sed -E -z 's/>\n.*\n<\//>\n<\//'  Input_file

2nd solution: With awk you could try following code. Written and tested with your shown samples only.

awk -v RS= '{sub(/>\n.*\n<\//,">\n<\/")} 1' Input_file

CodePudding user response:

Using sed

$ sed -Ei.bak '/^  <.*>/d' input_file
<test>
</test>
  • Related