Computing.Net > Forums > Unix > Replacing Consecutive pipes

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Replacing Consecutive pipes

Reply to Message Icon

Name: mailme_anil
Date: December 22, 2004 at 07:33:20 Pacific
OS: UNIX
CPU/Ram: 1gb
Comment:

Hi,

I am having a file which has 9 million records. Few records in between are having no value for a field resulting in two consecutive pipes(||). Because of this, the data load is getting rejected. I want to replace these consecutive pipes(||) with a space between them(| |). Please provide a solution for this.

Ex :
abc|123|aa||bcd|23^M

I want the above record as :
abc|123|aa| |bcd|23^M

Thanks,
Anil




Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: December 22, 2004 at 09:30:36 Pacific
Reply:

echo "abc|123|aa||bcd|23^M" | sed -e "s/||/| |/g"

sed -e "s/||/| |/g" INFILE > OUTFILE


0

Response Number 2
Name: thepubba
Date: December 22, 2004 at 09:32:56 Pacific
Reply:

Perhaps something like:

sed 's/||/| |/g' input.file > output.file



0

Response Number 3
Name: nails
Date: December 22, 2004 at 11:36:01 Pacific
Reply:

Jerry and David's sed solution works fine, except where you have two sequential null columns:

abc|123|aa|||bcd|23

My solution is this:

sed -e 's/|/|\&/g' -e 's/|\&|/| | /g' -e 's/\&//g' d2.file

1) replace each pipe symbol with a pipe symbol and a character not used in the data set. I chose & and escaped it cause sed uses it.

2) replace |\&| with a space

3) then delete all the unnneeded &

haven't thoroughly tested, but seems to work.



0

Response Number 4
Name: Jim Boothe
Date: December 22, 2004 at 14:22:56 Pacific
Reply:

That's a good catch, nails. But your code will insert an extra blank sometimes, such as before the bcd in this example:

in:   abc|123|aa||||bcd|23
out: abc|123|aa| | | | bcd|23

Following code will keep reprocessing a line as long as changes are being made:

sed -e :a -e 's/||/| |/g' -e ta


0

Response Number 5
Name: nails
Date: December 22, 2004 at 21:55:29 Pacific
Reply:

Jim:

That's a good catch of my catch. I really feel dumb not thinking of using a loop.

Nails


0

Related Posts

See More



Response Number 6
Name: Jim Boothe
Date: December 23, 2004 at 06:00:39 Pacific
Reply:

It's hard to get one up on the nails!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Deleting nth row Unix-sed- need help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Replacing Consecutive pipes

Replace Word www.computing.net/answers/unix/replace-word/6230.html

Sed, search/replace w/in range www.computing.net/answers/unix/sed-searchreplace-win-range/7707.html

Remove Pipe from file www.computing.net/answers/unix/remove-pipe-from-file/7706.html