Sponsored Content
Full Discussion: How can we do this!?
Top Forums UNIX for Beginners Questions & Answers How can we do this!? Post 302998653 by RavinderSingh13 on Monday 5th of June 2017 04:00:28 PM
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.
 
All times are GMT -4. The time now is 01:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy