How to replace symbol || with |“”| using sed Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to replace symbol || with |“”| using sed without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a file that uses | as a delimiter. Some of the records have empty cell as || (nothing inside). I am trying to replace them with |""| with following sed command:

sed -i 's/||/|""|/g' file

But the result wasn’t what I expected.

Input file

A|B|C|D|||E

Desired output:

A|B|C|D|""|""|E

Note that the beginning and ending of records doesn’t have “|”

Any help would be appreciated.

Answer

Just repeat the substitution until output doesn’t change:

$ echo 'A|B|C|D|||E' | sed ':X;s/||/|""|/g;tX'
A|B|C|D|""|""|E

where

  • :X sets the label X
  • t X go to label X if s/// was successful
We are here to answer your question about How to replace symbol || with |“”| using sed - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji