Search code examples
awkadditionconcatenationcut

Concatenate columns and adds digits awk


I have a csv file:

number1;number2;min_length;max_length
"40";"1801";8;8  
"40";"182";8;8  
"42";"32";6;8  
"42";"4";6;6  
"43";"691";9;9  

I want the output be:

4018010000;4018019999  
4018200000;4018299999  
42320000;42329999
423200000;423299999
4232000000;4232999999
42400000;42499999  
43691000000;43691999999  

So the new file will be consisting of:

column_1 = a concatenation of old_column_1 + old_column_2 + a number of "0" equal to (old_column_3 - length of the old_column_2)

column_2 = a concatenation of old_column_1 + old_column_2 + a number of "9" equal to (old_column_3 - length of the old_column_2) , when min_length = max_length. And when min_length is not equal with max_length , I need to take into account all the possible lengths. So for the line "42";"32";6;8 , all the lengths are: 6,7 and 8.

Also, i need to delete the quotation mark everywhere.

I tried with paste and cut like that:

paste -d ";" <(cut -f1,2 -d ";" < file1) > file2

for the concatenation of the first 2 columns, but i think with awk its easier. However, i can't figure out how to do it. Any help it's apreciated. Thanks!

Edit: Actually, added column 4 in input.


Solution

  • You may use this awk:

    awk 'function padstr(ch, len, s) {
       s = sprintf("%*s", len, "")
       gsub(/ /, ch, s)
       return s
    }
    BEGIN {
       FS=OFS=";"
    }
    {
       gsub(/"/, "");
       for (i=0; i<=($4-$3); i++) {
          d = $3 - length($2) + i
          print $1 $2 padstr("0", d), $1 $2 padstr("9", d)
       }
    }' file
    

    4018010000;4018019999
    4018200000;4018299999
    42320000;42329999
    423200000;423299999
    4232000000;4232999999
    42400000;42499999
    43691000000;43691999999