batman
batman

Reputation: 3685

Sed is not taking my regex

I have the following sed command ( for replacing certain contents with space/nothing):

sed "s/.* \[test] \(h\[W/L]-.*\)//g" file.log

but this gives me error like:

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

where I'm making the mistake?

2013-12-04 00:00:39,629 INFO  [test] (h[W/L]-75) <a>
adasdsdads
asdasdsddsaadsdasds
</a>


2013-12-04 00:00:39,629 INFO  [test] (h[W/L]-75) <a>

asdasdsasaasdas


a

asdas
</a>

The required op is:

<a>
adasdsdads
asdasdsddsaadsdasds
</a>


<a>

asdasdsasaasdas


a

asdas
</a>

Upvotes: 0

Views: 54

Answers (4)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's#.* \[test] (h\[W/L]-.*)##' file.log
  • other separator than default / in s///, here #
  • unescape () for the litterla use (opposite behaviour than [)
  • no use of g option, only 1 occurence per line

Upvotes: 0

Barmar
Barmar

Reputation: 780994

You need to escape the / inside the regexp:

sed "s/.* \[test] \(h\[W\/L]-.*\)//g" file.log

Otherwise, it's treated as the end of the regexp. Another solution is to use a different delimiter, that isn't used in your pattern:

sed "s|.* \[test] \(h\[W/L]-.*\)/||g" file.log

And if you're trying to match literal parentheses, they don't need to be escaped in sed. It should be:

sed "s|.* \[test] (h\[W/L]-.*)||g" file.log

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174706

You could try this sed command.

$ sed 's/.* \[test\] (h\[W\/L\]-.*) *//g' file
<a>
adasdsdads
asdasdsddsaadsdasds
</a>


<a>

asdasdsasaasdas


a

asdas
</a>

<space>* at the last helps to remove the spaces following the last ) bracket.

Upvotes: 1

nitishagar
nitishagar

Reputation: 9413

Try this:

 sed "s/.* \[test] \(h\[W\/L]-.*\)//g" file.log

Upvotes: 0

Related Questions