How can we do this!?

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

Not sure what's going on it is working fine for me, could you please try following things.

I- Check if there are any carriage characters are present or not in your Input_file by doing cat -v Input_file. If yes then remove them by adding gsub(/\r/,"") in starting of my previous post's solution.

II- If no carriage characters, then ,my second guess would be are you using Sun OS/Solaris? If yes then, on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Let me know how it goes then.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 06-05-2017
Thanks R.Singh. It did not work.

Is there any other way we can do this by reading the input file first --

something like --

Code:
cat Input_File | awk "Your and Rudi's logic"

.

Thanks
# 10  
Old 06-05-2017
Quote:
Originally Posted by Indra2011
Thanks R.Singh. It did not work.
Is there any other way we can do this by reading the input file first --
something like --
Code:
cat Input_File | awk "Your and Rudi's logic"

.
Thanks
Hello Indra2011,

awk could read any Input_file by its own no need to use cat here.
You have to show us the complete console output like what's going on there, without that we could only guess.

Also I am coming with this function approach for this question, where you could just mention like how many columns you want to verify.
Following is the code on same too:

1st Scenario: with your asked query.
Code:
awk -F, 'function check(val){do{;num=split(val, array,",");i=1;while(i<=num){if(!$array[i]){$array[i]=old[i];} else {old[i]=$array[i]};;i++};i="";print $0;;} while((getline)>0)}; check("1,2,3")' OFS=,   Input_file

2nd scenario: Input_file which I have created for checking purposes, a test file
Code:
##Input_file:
cat  Input_file
101,103,1,2,3
,,,2,3
,,,4,5
201,234,7,8,9
,,,4,5
,,,,5
  
awk -F, 'function check(val){do{;num=split(val, array,",");i=1;while(i<=num){if(!$array[i]){$array[i]=old[i];} else {old[i]=$array[i]};i++};i="";print $0;;} while((getline)>0)}; check("1,2,3,4")' OFS=,   Input_file

In this above code I have provided 1,2,3,4 fields so it will look for 1st, 2nd, 3rd and 4th fields here. Kindly do check the same and let me know if you have any queries.

EDIT: Adding a NON-one liner form of solution too successfully now.
Code:
awk -F, 'function check(val){
                                do{
                                        num=split(val, array,",");
                                        i=1;
                                        while(i<=num){
                                                        if(!$array[i]){
                                                                                $array[i]=old[i];
                                                                      }
                                                        else          {
                                                                                old[i]=$array[i]
                                                                      }
                                                                                i++
                                                     }
                                        i="";
                                        print
                                  }
                                while((getline)>0)
                            };
         check("1,2,3,4")
       ' OFS=,         Input_file

EDIT2: In above code you could change from !$array[i] to $array[i] == "" in case your Input_file may have zero too in any of the columns.

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-05-2017 at 05:33 PM.. Reason: Adding a NON-one liner form of solution too successfully now.
# 11  
Old 06-05-2017
Did any of the proposals given in this thread run error free? If yes, what did you change in post#7? If no, please post your command and its stdin and stderr output exactly as is, one to one, char by char, so we have a chance to analyse.
# 12  
Old 06-05-2017
Hi Ravinder,

Many many thanks for help. This was what I get while running the code.

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

awk 'test1{$1=OLD1} test2{$2=OLD2} test3{$3=OLD3} {OLD1=$1; OLD2=$2; OLD3=$3} 1' FS="," OFS="," test
101,103,1,2,3
,,,2,3
,,,4,5
201,234,7,8,9
,,,4,5

Thanks
# 13  
Old 06-05-2017
What's your OS and shell?
This User Gave Thanks to RudiC For This Post:
# 14  
Old 06-05-2017
Quote:
Originally Posted by Indra2011
Hi Ravinder,
Many many thanks for help. This was what I get while running the code.
[CODE]disco,mum $ awk '!$1{$1=OLD1} !$2{$2=OLD2} !$3{$3=OLD3} {OLD1=$1; OLD2=$2; OLD3=$3} 1' FS="," OFS="," test
awk 'test1{$1=OLD1} test2{$2=OLD2} test3{$3=OLD3} {OLD1=$1; OLD2=$2; OLD3=$3} 1' FS="," OFS="," test
Thanks
Hello Indra2011,

I haven't seen your second command in any of the solutions provided in this thread?
It will NOT give any results as variables named test1, test2, test3 are never having values.

Could you please try my code(s) in POST#10 and let us know how it goes then?

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

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