zwlayer
zwlayer

Reputation: 1824

Why Sed cannot replace lines that contains . + and *

I have a large file and some of the lines are as follows:

....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*

I want to replace ....+UNKNOWN part with the ...+PUNCT

To do that, I did the following:

sed 's/\.\.\.\.\+\*UNKNOWN\*/\.\.\.\+PUNCT/g' myfile.out > myfile_result.out

However, none of the lines gone.

grep -F '...+*UNKNOWN*' myfile.out 

....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*
....    ....+*UNKNOWN*

I couldn't find my error, could you help me about ?

Upvotes: 1

Views: 61

Answers (3)

RavinderSingh13
RavinderSingh13

Reputation: 133770

Could you please try following.

sed 's/+\*UNKNOWN/+PUNCT/'  Input_file

Explanation of + with an example:

echo "This is an example: 123 test 123" | sed -E  's/^[^0-9]*([0-9]+).*/\1/'
123

Let's say we have a line This is an example: 123 test 123 so here [0-9]+ will match very first set of digits which are coming together and then by keeping them in sed 's memory I am printing them by using \1(escaping 1 here again to let sed know it is the memory sequence I am calling here).

Upvotes: 0

Bhawan
Bhawan

Reputation: 2501

\+ in sed means one or more match of the expression preceeded just before '+', means no + literal.
To match a literal '+' , use + to match the plus in literal.

Modified expression:

sed 's/\.\.\.\.+\*UNKNOWN\*/\.\.\.\+PUNCT/g' myfile.out > myfile_result.out

Upvotes: 0

choroba
choroba

Reputation: 242383

In some versions of sed, \+ has a special meaning, use + to match the plus literally.

Upvotes: 1

Related Questions