0

How can I replace below string \/ by | like:

bssapi_6 0\/0\/0\/9\/9

bssapi_6 0|0|0|9|9
1
  • 1
    Like this? echo bssapi_6 0\/0\/0\/9\/9 | sed 's/\//|/g;' EDIT: I used echo since most of the answers used other ways Jun 22, 2021 at 16:27

3 Answers 3

6

With liberal amounts of escaping applied:

sed 's/\\\//|/g' < inputfile

Making it more readable by using @ as a separator:

sed 's@\\/@|@g' < inputfile
2
  • 2
    hehe I guess it now depends whether the OP prefers _ or @
    – mattb
    Jun 22, 2021 at 12:56
  • 1
    The same answer as so often: The separator used by sed's s command is determined by the character immediately following it. One can use any character here. Jun 22, 2021 at 19:04
3

Either:

sed 's/\\\//|/g' your_file

or, using _ as a pattern delimiter so we don't need to escape the /:

sed 's_\\/_|_g' your_file
0

Using awk:

for file input

$ cat input
bssapi_6 0\/0\/0\/9\/9

The following would do:

awk '{gsub(/\\\//,"|"); }1' input

Because we want to replace \/ by |, here both \ and / are escaped as \\ and \/ respectively. Therefore regex is \\\/.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .