use awk to replace empty fields with the latest nonempty field

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use awk to replace empty fields with the latest nonempty field
# 1  
Old 10-27-2009
use awk to replace empty fields with the latest nonempty field

Hi suppose I have a csv file like this

usa,         ,       , blue
japan,   red, yellow,green
india,        ,  yellow,blue
china,   blue, yellow, green

I want the output to be(replace everything, including empty data, with the most recent data):

usa,     blue, blue, blue
japan,   green, green,green
india,    blue,  blue,blue
china,   green, green, green

how to do this in awk? thanks.

Last edited by Franklin52; 10-27-2009 at 10:20 AM.. Reason: Please use code tags!
# 2  
Old 10-27-2009
Try this:

awk -F, 'NR>1{for(i=2;i<NF;i++){$i=$NF}}1' OFS="," file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk empty fields

Hello I have a file like this a,b,c,1,2,3,d,e,f,,,,g,h,i,,,,j,k,l and using awk 'FS="'"{print $9,$10,$11}' does not work as I was hoping. I would like the empty fieds, i.e. between the two comma to be interpreted as a zero. is this possible? I would like to get f 0 0 out of the above... (1 Reply)
Discussion started by: garethsays
1 Replies

2. Shell Programming and Scripting

awk to identify empty fields in line

I am trying to use awk to identify and print out records in fields that are empty along with which line they are in. I hope the awk below is close, it runs but nothing results. Thank you :). awk awk -F'\t' 'FNR==NR ~ /^*$/ { print "NR is empty" }' file file 123 GOOD ID 45... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

How to detect empty field in awk ?

Hi ! programmers I have a need of detecting empty field in file my file looks like this 40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31... (7 Replies)
Discussion started by: Dona Clara
7 Replies

6. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

7. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

awk ignores fields with only spaces or empty

Hi, Does any one know how to avoid the scenario where awk ignores the fields having only spaces or empty fields? for instance, Data: "a","b","c","d",""," " code: awk -F, '{ print NF }' File the output I get is 4 instead of 6 do you know how to avoid this? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

9. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question