Replace null values in csv with zero


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace null values in csv with zero
# 1  
Old 09-02-2015
Replace null values in csv with zero

Hi,

i have another issue:

i have three files:

FILE 1
Code:
ServiceEventHandler,
Processed,Percentage
5285337,100%

FILE 2
Code:
Wallet,
Processed,Percentage
5285337,100%

FILE 3
Code:
TIME
< 1m
> 1m - 2m
> 2m - 3m
> 3m - 5m
> 5m - 10m
> 10m - 15m
> 15m - 20m
> 20m - 30m
> 30m

i used this code to join all the files

Code:
paste -d',' file3 file1 file2 > file4.csv

please see output i get

Code:
,ServiceEventHandler,,WalletHandler,
,Processed,Percentage,Processed,Percentage
TIME,2839390,100%,39211657,100%
< 1m,,
> 1m - 2m,,
> 2m - 3m,,
> 3m - 5m,,
> 5m - 10m,,
> 10m - 15m,,
> 15m - 20m,,
> 20m - 30m,,
> 30m,,

My desired output is shown below:

Code:
,ServiceEventHandler,,WalletHandler,
TIME,Processed,Percentage,Processed,Percentage
< 1m,2839390,100%,39211657,100%
> 1m - 2m,0,0,0,0
> 2m - 3m,0,0,0,0
> 3m - 5m,0,0,0,0
> 5m - 10m,0,0,0,0
> 10m - 15m,0,0,0,0
> 15m - 20m,0,0,0,0
> 20m - 30m,0,0,0,0
> 30m,0,0,0,0
TOTAL,2839390,100%,39211657,100%

Note: the total should be the sum of fields in Processed and Percentage

Please help. need it badly

Thanks
# 2  
Old 09-02-2015
This is a very rudimentary approach, no bells, no whistles, no error checking, some function(s) might have been defined and used, taking advantage of awk's default behaviour where possible, but for the samples given it (almost) yields the (what I think is the) desired result (the desired result posted can't possibly have been generated with your sample input!). Try
Code:
awk '
NR==1   {getline L1 < F1
         getline L2 < F2
         print "", L1, L2
        }
        {getline L1 < F1
         split (L1, T)
         SUM1 += T[1]
         PRC1 += T[2]
         getline L2 < F2
         split (L2, T)
         SUM2 += T[1]
         PRC2 += T[2]

         print $0, L1, L2
         L1=L2="0,0"
        }

END     {print "TOTAL", SUM1, PRC1 "%", SUM2, PRC2 "%"}
' FS="," OFS="," F1="file1" F2="file2" file3
,ServiceEventHandler,,Wallet,
TIME,Processed,Percentage,Processed,Percentage
< 1m,5285337,100%,5285337,100%
> 1m - 2m,0,0,0,0
> 2m - 3m,0,0,0,0
> 3m - 5m,0,0,0,0
> 5m - 10m,0,0,0,0
> 10m - 15m,0,0,0,0
> 15m - 20m,0,0,0,0
> 20m - 30m,0,0,0,0
> 30m,0,0,0,0
TOTAL,5285337,100%,5285337,100%

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check for null values in a columns. I have dozen of CSV files in a directory.

Hi Folks, I'm trying to write a simple file sanity check script. I have a directory with dozen CSV files containing id,edname,firstname,lastname,suffix,email. I like to write a awk script to check if first field contain a number and is not empty. and fields number 3,4 & 6 are not empty and... (3 Replies)
Discussion started by: dc34684
3 Replies

2. 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

3. Shell Programming and Scripting

Multiple columns replace with null values.

I am trying to replace the partcular columns(Col3,col5,col20,col44,col55,co56,col59,col60,col61,col62,col74,col75,col88,col90,col91,col93,col94,col95) with empty Input file Col1,col2,col3,col4,col5------,col100 1,2,3,4,5,---------,100 3,4,5,6,7,---------,300 Output : ... (3 Replies)
Discussion started by: onesuri
3 Replies

4. Shell Programming and Scripting

Replace a field where values are null in a file.

Hi, I've a pipe delimited file and wanted to replace the 3rd field to 099990 where the values are null. How can I achieve it using awk or sed. 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S... (12 Replies)
Discussion started by: rudoraj
12 Replies

5. Shell Programming and Scripting

File values alwaya null

Hi All , below is my shell program. !/bin/sh set -x #---------------------------------------------------------------------------------------- # Program : weekly_remove_icd_file.sh # Author : # Date : 04/06/2013 # Purpose : Execute the script to... (3 Replies)
Discussion started by: krupasindhu18
3 Replies

6. Shell Programming and Scripting

How to use sort with null values?

Hello everyone I am doing a join command. Obviously, before I need two files sorted first. ( Both files have headers and have about 2 million lines each one ) The problem is, one of the files has null values in the key to sort (which is the first filed ). For example I have the original... (4 Replies)
Discussion started by: viktor1985
4 Replies

7. Shell Programming and Scripting

sorting null values

Hi I have a file with the values abc res set kls lmn ops i want to sort this file with the null values at the bottom of the file OUTPUT should look like this abc kls lmn ops (6 Replies)
Discussion started by: vickyhere
6 Replies

8. UNIX for Advanced & Expert Users

How to Compare Null values??

Hi, Can someone help me comparing Null values. Scenario is as follows: I have a variable which "cache_prd" which can have either some integer or nothing(Null) if it is integer I have to again do some comparision but these comparisons give me this error:( "line 32: [: 95: unary operator... (3 Replies)
Discussion started by: Yagami
3 Replies

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

10. Shell Programming and Scripting

identifying null values in a file

I have a huge file with 20 fileds in each record and each field is seperated by "|". If i want to get all the reocrds that have 18th or for that matter any filed as null how can i do it? Please let me know (3 Replies)
Discussion started by: dsravan
3 Replies
Login or Register to Ask a Question