Kshitiz Sharma
Kshitiz Sharma

Reputation: 18597

Why doesn't sed interpret this regex properly?

echo "This is a test string" | sed 's/This/\0/'

First I match substring This using the regex This. Then I replace the entire string with the first match using \0. So the result should be just the matched string.

But it prints out the entire line. Why is this so?

Upvotes: 0

Views: 85

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/.*\(PatThis\).*/PatThat/'

or

se '/PatThis/ s/.*/PatThat/'

In your request "PatThis" and "PatThat" are the same contain ("This"). In the comment (

I need to select a number using \d\d\d\d and then use it as replacement

) you have 2 different value for the pattern PatThis and PatThat the \1 is not really needed because you know the exact contain (unless 'PatThis' is a regex with special char like \ & ? .)

Upvotes: 0

pfnuesel
pfnuesel

Reputation: 15310

You don't replace the whole string with \0, just the pattern match, which is This. In other words, you replace This with This.

To replace the whole line with This, you can do:

echo "This is a test string" | sed '/This/s/.*/This/'

It looks for a line matching This, and replaces the whole line with This. In this case (since there is only one line) you can also do:

echo "This is a test string" | sed 's/.*/This/'

If you want to reuse the match, then you can do

echo "This is a test string" | sed 's/.*\(This\).*/\1/'

\( and \) are used to remember the match inside them. It can be referenced as \1 (if you have more than one pair of \( and \), then you can also use \2, \3, ...).

In the example above this is not very helpful, since we know that inside \( and \) is the word This, but if we have a regex inside the parentheses that can match different words, this can be very helpful.

Upvotes: 3

Related Questions