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
Replacing the Spaces Amey Joshi UNIX for Dummies Questions & Answers 3 11-11-2008 11:15 PM
need help in replacing spaces in a file sais Shell Programming and Scripting 7 10-06-2008 09:24 AM
Replacing character in file with null value HLee1981 Shell Programming and Scripting 18 07-18-2006 01:33 PM
Handling null input... DrRo183 Shell Programming and Scripting 2 04-11-2006 01:32 AM
finding & replacing blank rows/spaces in a file Gerry405 SUN Solaris 2 07-21-2005 04:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
replacing spaces with null or 0 in the input file

hi
i have records in my input file like this

aaa|1234||2bc||rahul|tamilnadu
bba|2234||b4c||bajaj|tamilnadu


what i am expecting is in between two pipes if there is no character it should be replaced with null or 0

so my file will look like this

aaa|1234|null|2bc|0|rahul|tamilnadu
bba|2234|null|b4c|0|bajaj|tamilnadu


how to do it

can anyone explain

thanks in advance
  #2 (permalink)  
Old 01-06-2009
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
Code:
$ cat tri.txt
aaa|1234||2bc||rahul|tamilnadu
bba|2234||b4c||bajaj|tamilnadu

$ awk 'BEGIN{OFS=FS="|"}{ for(k=0;k<=NF;k++)
if ( $k == "" )
$k="NULL"
print $0}' tri.txt

aaa|1234|NULL|2bc|NULL|rahul|tamilnadu
bba|2234|NULL|b4c|NULL|bajaj|tamilnadu
  #3 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,308
Code:
sed -e 's/||/|null|/g' -e 's/||/|null|/g' FILE
  #4 (permalink)  
Old 01-06-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
thanks a lot it worked
but i wanted null and 0 to be populated
is there any way for the same

thanks in advance
  #5 (permalink)  
Old 01-06-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,047
Code:
#! /usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	my @tmp=split("[|]",$_);
	map{$_=$_||"null"} @tmp;
	print join "|",@tmp;
}
Sponsored Links
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 11:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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