10

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'
Pro Backup
  • 729
  • 14
  • 34
Talespin_Kit
  • 20,830
  • 29
  • 89
  • 135
  • 2
    you can't and there's no need to – Ahmed Masud Jul 19 '13 at 08:56
  • @AhmedMasud I'm guessing that the real-world example has a more complicated search pattern containing slashes. The real question is probably how to escape a string for pattern matching, which is covered elsewhere (e.g http://backreference.org/2009/12/09/using-shell-variables-in-sed/). – cmbuckley Jul 19 '13 at 09:07
  • 7
    @AhmedMasud: yes you can, and the question demonstrates a need. – tripleee Jul 19 '13 at 09:19

2 Answers2

5

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.

devnull
  • 118,548
  • 33
  • 236
  • 227
2

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.

Priv Acyplease
  • 418
  • 5
  • 6