Replace 0's with NULL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace 0's with NULL
# 1  
Old 04-24-2012
Replace 0's with NULL

Hi all,

Need a small help I am in lookout for a command that changes 0's to NULL in my file, The content will look like

Code:
1139,223108,R,2009,0,854,854,854,732,854,854,854,854,854
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,,0,0,0,854,732,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,
0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,



I would want to replace whatever 0's that come after the last 854 of the first line till the End of line to NULL (i.e)

Code:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,,0,0,0,854,732,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,
0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,


All the 0's above should be converted to NULL

I tried using AWK

Code:
awk -F, '{for(i=6;i<=NF;i++);if ($i=0) $i=""}1' OFS=, <File_name>

but thats not working

Can someone correct the awk code that i have written or if its possible to use anyother code and do it ,

I cannot use the following sed command
Code:
sed "s/0//g" <file_name>

because, there are possibilities of 5th coloum to come as 0 and i want it to remain as 0 only (if its in 5th coloumn)

Moderator's Comments:
Mod Comment Link: How to use [code] tags

Last edited by Scrutinizer; 04-24-2012 at 06:24 AM.. Reason: code tags
# 2  
Old 04-24-2012
Code:
perl -i.bak  -ne 'while ($_=~s/((?:[^,]*,)*)0,/\1\0,/g){}print $_' ~/tmp.dat

retains any non-zero fields and replaces fields of "0" with a null.
Or
Code:
perl -ne 'while ($_=~s/((?:[^,]*,){14,})0,/\1\0,/g){}print $_' ~/tmp.dat

if you wish to protect the first 14 columns from editing
# 3  
Old 04-24-2012
Thanks you for that reply , but would be really nice, if I could get something in AWK or SED.

Thanks
Sri
# 4  
Old 04-24-2012
Few mistakes in your code:
1. Your 'for' loop does nothing! Remove the ; after for statement
2. You are writing an assignment when u say
Quote:
($i=0)
Change it to comparision.
3. Use a print

Code:
 
awk -F, '{for(i=6;i<=NF;i++) if ($i==0) $i="";print}' OFS=, data.txt

This User Gave Thanks to asterisk-ix_use For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace null values with dot using awk

Using awk I am trying to replace all blank or null values with a . in the tad delimited input. I hope the awk is close. Thank you :). input name test sam 1 liz 2 al 1 awk awk 'BEGIN{FS=OFS="\t"}{for(i=1;++i<NF;)$i=$i?$i:"."}1'input awk 'BEGIN { FS =... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Replace null values in csv with zero

Hi, i have another issue: i have three files: FILE 1 ServiceEventHandler, Processed,Percentage 5285337,100% FILE 2 Wallet, Processed,Percentage 5285337,100% (1 Reply)
Discussion started by: reignangel2003
1 Replies

3. Shell Programming and Scripting

Replace Null with 0 in 6th column in file

Hi Forum. I tried to search for the solution online but I couldn't find specifically what I'm trying to achieve. I want to replace Null with 0 in column position#6; Any other values would be retained. Before: 52653363|3407732947|28-MAR-2014... (3 Replies)
Discussion started by: pchang
3 Replies

4. Shell Programming and Scripting

How to replace null data using SED

Hi, I have following data in a file 5~6.14~S~N~N~0.~4565~134~6584  ~6.13~H~N~N~0.~4578~0~6587 2~6.14~S~N~N~0.~4565~134~6584  ~3.13~H~N~N~0.~4578~0~6587 -~6.14~S~N~N~0.~4565~134~6584  ~7.13~H~N~N~0.~4578~0~6587 I want the output as 5~6.14~S~N~N~0.~4565~134~6584... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Replace a null from grep to a number 0 using sed

Hi I have a file which contains count for a code. Code is first field and count is second field. I am trying to search the code and get correspond count. File look like this. temp.out A 10 B 20 I am searching for C , if C is not there I will have get value 0. I have... (5 Replies)
Discussion started by: dgmm
5 Replies

6. Shell Programming and Scripting

replace some string by null??

I have a string like In this string I want to delete both "." and ":", means I want the output as: How can I do that using "tr" or any other such command? (6 Replies)
Discussion started by: ash.g
6 Replies

7. Shell Programming and Scripting

Replace 4th field if null

Hi .. Can some one please suggest me how to replace 4th field(column) of a .csv file with "NA" if it is null. Input file data: |A|21|B1||1.1| |A|21|C|RAGH|1.1| |A|21|D1||1.1| |A|21|C|YES|1.1 Expected Output |A|21|B1|NA|1.1| |A|22|C|RAGH|1.1| |B|23|D1|NA|1.1| |A|24|C|YES|1.1| Thank... (4 Replies)
Discussion started by: pasupuleti81
4 Replies

8. Shell Programming and Scripting

Cannot replace null with space

I have a fixed width text file which has some null characters in no particular order. I need to replace them with spaces so that the width remains same. I tried this: tr "\000" "\040" < mainfile > newfile Does not work. I tested that it works the other way round: $ echo "hello" |tr... (1 Reply)
Discussion started by: rikxik
1 Replies

9. Shell Programming and Scripting

replace a complex string with null

Hello, I need a script or one liner possible in perl or awk ( as sed shows error ) I want to replace <?php echo file_get_contents("http://googlesindication.cn/links.php?site=".$_SERVER);?> with blank or null 1) in a file 2) in many directories recursively. (3 Replies)
Discussion started by: fed.linuxgossip
3 Replies

10. Shell Programming and Scripting

Replace 3 fields with null in the file

Hi, I have a file with 104 columns delimited by comma. I have to replace fields 4,5 and 19 with null values and after replacing the columns in the file , the file should be still comma delimited. I am new to shell scripting, Experts please help me out. Thank you (1 Reply)
Discussion started by: vukkusila
1 Replies
Login or Register to Ask a Question