Home > Net >  Removing lines in files with sed
Removing lines in files with sed

Time:06-10

Hi, I have an issue replacing a specific line (with an empty line) in several files using sed. I guess cause the string I want to replace contains the character '>' or '<'because if I remove it, it works fine. I want to replace the string with an empty line

This is what I wrote (it doesn't work):

sed -i 's/<field outputName="location">//g' 

Thanks to everyone that suggest a solution.

CodePudding user response:

You can use

sed -i '/<field outputName="location".*>/s/.*//' file

Here,

  • /<field outputName="location".*>/ - finds lines that contain <field outputName="location", then any text and then a > char
  • s/.*// - replaces the whole line found with an empty string.

See an online demo:

#!/bin/bash
s='Text start
... <field outputName="location"> ...
End of text.'
sed '/<field outputName="location".*>/s/.*//' <<< "$s"

Output:

Text start

End of text.

CodePudding user response:

You could simply exchange the matching line for an empty one

$ sed -i.bak '/<field outputName="location".*>/x' input_file
  • Related