You're viewing a comment by Yogesh and its responses.

February 13, 2009, 12:34

In #31, if the pattern (foo) comes more than two times in the line then it replaces the second last one. As an example

echo "this is first foo, this is second foo another foo quux" | sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'

then output will be:
this is first foo, this is second bar another foo quux

Which doesn't fulfill our requirements.

Regards,
Yogesh

Comment Responses

Anirudh Permalink
February 02, 2016, 11:56

You should do such operations without resorting to greediness of regexes

sed 's/foo/bar/'

changes the first "foo" to "bar", no matter how many times "foo" is in the pattern space.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "security_95": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.