Math Lover
Math Lover

Reputation: 148

Removing lines in files with sed

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.

Upvotes: 1

Views: 48

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

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.

Upvotes: 1

Related Questions