How can we do this!?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How can we do this!?
# 1  
Old 06-05-2017
How can we do this!?

Hi,

I have an input file which looks like

Code:
101,103,1,2,3
,,,2,3
,,,4,5
201,234,7,8,9
,,,4,5

I would like top have output like, Is this possible?

Code:
101,103,1,2,3
101,103,1,2,3
101,103,1,4,5
201,234,7,8,9
201,234,7,4,5

Thanks a lot,

Last edited by Indra2011; 06-05-2017 at 03:44 PM..
# 2  
Old 06-05-2017
Looks like there's a comma too many in the lines with missing elements. Assuming this is an error, try
Code:
awk '!$1{$1=OLD1} !$2{$2=OLD2} {OLD1=$1; OLD2=$2} 1' FS="," OFS="," file
101,103,1,2
101,103,2,3
101,103,4,5
201,234,8,9
201,234,4,5

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-05-2017
Thanks a lot Rudi. You are right, the input file was wrong. i have corrected it now. Will this code of yours even work now if i just include
Code:
$3=OLD3

in your code.
# 4  
Old 06-05-2017
You're half way there. Post the full solution, and we'll comment on it.
# 5  
Old 06-05-2017
Hi RudiC,

Will this code work?. Just included $3 component in your logic.

Code:
awk '!$1{$1=OLD1} !$2{$2=OLD2} !$3{$3=OLD3} {OLD1=$1; OLD2=$2; $3=OLD3} 1' FS="," OFS="," file

Thanks
# 6  
Old 06-05-2017
Quote:
Originally Posted by Indra2011
Hi RudiC,
Will this code work?. Just included $3 component in your logic.
Code:
awk '!$1{$1=OLD1} !$2{$2=OLD2} !$3{$3=OLD3} {OLD1=$1; OLD2=$2; $3=OLD3} 1' FS="," OFS="," file

Thanks
Hello Indra2011,

Good try Smilie, a little adjustment to your solution will be, don't change $3 because it will make it's value to variable as OLD3 which will be dangerous on the first line itself when variable OLD3 will be NULL and it will be assigned to 3rd column, then it will never be a NON-NULL value, so try.
Code:
awk '!$1{$1=OLD1} !$2{$2=OLD2} !$3{$3=OLD3} {OLD1=$1; OLD2=$2; OLD3=$3} 1' FS="," OFS=","  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 06-05-2017
Hi Ravinder Singh,

I am getting a syntax error. Also, my error look like this


Code:
awk: file{$1=OLD1} file{$2=OLD2} file{$3=OLD3} {OLD1=$1; OLD2=$2; OLD3=$3} 1 
awk : ^ syntax error

Please suggest, how can I correct it . is it due to
Code:
1' file

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question