The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sort truncates line when they contain nulls ArthurWaik Shell Programming and Scripting 6 04-22-2008 02:29 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
Blanks vs: Nulls ebock High Level Programming 1 02-12-2008 05:16 PM
GREPing for Nulls Dr. DOT UNIX for Dummies Questions & Answers 4 09-17-2004 03:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-22-2008
novice1324 novice1324 is offline
Registered User
  
 

Join Date: Feb 2008
Location: SFO, Ca, US
Posts: 10
Replacing nulls using sed

Hi,

I am trying to replace nulls with spaces in every record of a '|' delimited file. I used this command -

cat input.dat | sed 's/||/| |/g' > output

But if a space appears twice '| | |' , then only the first null is getting replaced with a space. The second one remains as a null. I guess, this may be because I am not specifying the delimiter and checking for nulls. Can anyone help me out in this. Thanks a lot..!!!
  #2 (permalink)  
Old 02-22-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by novice1324 View Post
Hi,

I am trying to replace nulls with spaces in every record of a '|' delimited file. I used this command -

cat input.dat | sed 's/||/| |/g' > output

But if a space appears twice '| | |' , then only the first null is getting replaced with a space. The second one remains as a null. I guess, this may be because I am not specifying the delimiter and checking for nulls. Can anyone help me out in this. Thanks a lot..!!!
Code:
sed 's/||/| |/g;s/||/| |/g' input.dat > output
  #3 (permalink)  
Old 02-23-2008
JamesByars JamesByars is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 124
I read that answer with interest. It does answer the OP's question.

But just on a side issue, why do we have to do this twice? Is it because the way sed works is that it will not reoperate on something it has just worked on in the same command?

i.e.

for the line:
|||

the command
Code:
sed 's/||/| |/g'
works by doing
| ||

and because the second | has been operated on, sed will not evaluate it, and therefore the new line containing || is not picked up. Is this right?

thanks
  #4 (permalink)  
Old 02-24-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by JamesByars View Post
I read that answer with interest. It does answer the OP's question.

But just on a side issue, why do we have to do this twice? Is it because the way sed works is that it will not reoperate on something it has just worked on in the same command?

i.e.

for the line:
|||

the command
Code:
sed 's/||/| |/g'
works by doing
| ||

and because the second | has been operated on, sed will not evaluate it, and therefore the new line containing || is not picked up. Is this right?

thanks
Yes something like that. Once the pattern is treated, sed will stream further and look for the next mathing pattern.
  #5 (permalink)  
Old 02-25-2008
novice1324 novice1324 is offline
Registered User
  
 

Join Date: Feb 2008
Location: SFO, Ca, US
Posts: 10
Thanks Vino for that answer.

But, having to do it twice will replace two consecutive nulls. This means that we have to repeat this operation as many times as the maximum number of consecutive nulls appearing in the row.

Is there a way to make it replace nulls by giving it the delimiter '|' and asking it to replace null values?

Again, thanks for your time.
  #6 (permalink)  
Old 02-25-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by novice1324 View Post
Thanks Vino for that answer.

But, having to do it twice will replace two consecutive nulls. This means that we have to repeat this operation as many times as the maximum number of consecutive nulls appearing in the row.
I dont quite get what you are saying. But running that sed statement once will replace all null's with a whitespace. All you need is 's/||/| |/g;s/||/| |/g'.

Quote:
Originally Posted by novice1324 View Post
Is there a way to make it replace nulls by giving it the delimiter '|' and asking it to replace null values?

Again, thanks for your time.
I think awk can do it.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0