Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the 'sed' slash (/) delimiter in insert command? [duplicate]

Tags:

sed

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'
like image 584
Talespin_Kit Avatar asked Jul 19 '13 08:07

Talespin_Kit


People also ask

How do you specify a delimiter in sed?

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.

How do you escape a forward slash in Linux?

You need to escape the / as \/ . The escape ( \ ) preceding a character tells the shell to interpret that character literally.

What is backslash in sed?

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: \\ .


2 Answers

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.

like image 84
devnull Avatar answered Oct 05 '22 05:10

devnull


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.

like image 30
Priv Acyplease Avatar answered Oct 05 '22 06:10

Priv Acyplease