Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing multiple occurrences of a word using sed in unix

Tags:

unix

sed

I have a requirement in unix to replace an occurrence of word with a space. My File looks something like below. I need to replace |NA| with a space

File Format

1234|NA|NA|abcd|xyz
2345|NA|NA|NA|lmn
456|NA|abcd|xya|ggh

Expected Output

1234| | |abcd|xyz
2345| | | |lmn
456| |abcd|xya|ggh

I am using the following command but it only replaces the very first occurrence

sed 's/|NA|| |/g'
like image 287
Santosh Avatar asked Dec 18 '22 16:12

Santosh


1 Answers

While the g modifier does make "global" replacements, the replacements must be non-overlapping. When overlapping replacements are required, one must loop:

$ sed ':a; s/|NA|/| |/g; ta' file.txt
1234| | |abcd|xyz
2345| | | |lmn
456| |abcd|xya|ggh

The above was tested on GNU sed. For BSD (OSX) sed (Hat tip: Jonathan Leffler), the label a must occur only at the end of a command string:

sed -e ':a' -e ' s/|NA|/| |/g; ta' file.txt

How it works

  • :a creates a label a.

  • s/|NA|/| |/g performs the substitution that you want but only for non-overlapping instances of |NA|.

  • ta tells sed to jump to label a if the preceding substitution command resulted in any changes to the line. In this way, the substitution command is repeated as many times as necessary to replace every occurrence of |NA|.

like image 120
John1024 Avatar answered Feb 24 '23 10:02

John1024