How to reverse sed output? How to reverse sed output? unix unix

How to reverse sed output?


Try using back references:

sed 's/.*\(searchstring\).*/___\1___/'

The .*'s around the search string will match everything but the string, and the parentheses tell sed to remember what it matched. You can refer to the first matched string with \1.

Here's an example (replacing everything but 'bar baz'):

$ echo "foo bar baz qux" | sed 's/.*\(bar baz\).*/___\1___/'___bar baz___

You can replace 'bar baz' with whatever pattern you like in the above; I just used a basic string for simplicity.