Changing the delimiter slash (/
) to pipe (|
) in the substitute command of sed
works like below
echo hello | sed 's|hello|world|'
How can I change the delimiter slash (/
) to pipe (|
) in the sed
insert command below?
echo hello | sed '/hello/i world'
sed Substitution Using different delimitersAny character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter itself can be used as a literal character if it is preceded by a backslash.
You need to escape the / as \/ . The escape ( \ ) preceding a character tells the shell to interpret that character literally.
You can use Unix tools like grep or sed to search all files that match a pattern. Then you can replace that pattern. But you have to remember that a backslash is a special character. It's used as an escape hatch to escape other expressions. Matching a backslash means you have to double it: \\ .
I'm not sure what is intended by the command you mentioned:
echo hello | sed '/hello/i world'
However, I presume that you want to perform certain action on lines matching the pattern hello
. Lets say you wanted to change the lines matching the pattern hello
to world
. In order to accomplish that, you can say:
$ echo -e "something\nhello" | sed '\|hello|{s|.*|world|}'
something
world
In order to match lines using a regexp, the following forms can be used:
/regexp/
\%regexp%
where %
may be replaced by any other single character (note the preceding \
in the second case).
The manual provides more details on this.
The answer to the question asked is:
echo hello | sed '\|hello|i world'
That is how you would prepend a line before a line matching a path, and avoid Leaning Toothpick Syndrome with the escapes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With