0

I have a pattern I am trying to match:

<x>anything</x>

I am trying to replace 'anything' (which can be any text, not the text anything - (.*)) with 'something' so any occurrences would become:

<x>something</x>

I am trying to use the following sed command:

sed "s/<x>.*</x>/<x>something</x>/g" file  

I am getting the following error:

sed: -e expression #1, char 19: unknown option to `s'

Can someone point me in the right direction?

Riley Kidd
  • 195
  • 1
  • 1
  • 4
  • possible duplicate of [Sed - unknown option to \`s'](http://stackoverflow.com/questions/9366816/sed-unknown-option-to-s) – tripleee Aug 10 '14 at 08:29

2 Answers2

2

This might work for you (GNU sed):

sed -r 's/(<x>)[^<]*/\1something/g' file

This looks to replace <x> and something which is not a < by <x>something repeatedly on the same line.

N.B. .* is greedy and may well swallow up further tags on the same line.

potong
  • 55,640
  • 6
  • 51
  • 83
0

The slashes in the closing XML tags are confusing it. Try escaping them like this:

sed "s/<x>.*<\/x>/<x>something<\/x>/g" file

You can apparently also use an equals sign which I'd never seen before. I'll be changing a bunch of scripts when I get to work!

chooban
  • 9,018
  • 2
  • 20
  • 36