0

I'm trying to write a CSV join program in AWK which joins the first.csv file with the second.csv file. The program now works perfectly fine, assuming the the number of rows in both files are the same.

The problem arises when one of the files contains more rows than the other; in this case, I'd have to add multiple number of commas (which depends to the number of fields in input files), to the file with less number of rows, so that the columns are not misplaced.

The question is, How can I create and assign strings containing different number of commas? for instance,

if: NumberOfFields==5; Then, I want to create string ",,,,," and add it to an Array[i].

Cyrus
  • 84,225
  • 14
  • 89
  • 153
MPAK
  • 39
  • 2
  • 5
  • 2
    Make a long string of commas in your `BEGIN` block then use `substr()` to grab as many as you need from it. – Mark Setchell Sep 05 '15 at 07:49
  • Or make an array in your `BEGIN` block like this, `pad[1]=","; pad[2]=",,"` and then just use `pad[5]` to get `",,,,,"`. – Mark Setchell Sep 05 '15 at 08:02
  • `printf ",%.0s" {1..5}`. example from [how-can-i-repeat-a-character-in-bash](http://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash) – amdixon Sep 05 '15 at 08:27

2 Answers2

4

Here is another answer with sample code using your variable and array name.

BEGIN {
    NumberOfFields=5;
    i=1;
    Array[i] = gensub(/0/, ",", "g", sprintf("%0*d", NumberOfFields, 0));
    print Array[i];
}

Run it with awk -f x,awk where x.awk is the above code in a text file. Be aware that it always prints at least 1 comma, even if you specify zero.

Fred
  • 564
  • 10
  • 21
1
$ awk -v num=3 'BEGIN {var=sprintf("%*s",num,""); gsub(/ /,",",var); print var}'
,,,
$

Use an array instead of var if/when you like. Note that unlike another solution posted the above will work with any awk, not just gawk, and it will not print any commas if the number requested is zero:

$ awk -v num=0 'BEGIN {var=sprintf("%*s",num,""); gsub(/ /,",",var); print var}'

$

The equivalent with GNU awk and gensub() would be:

$ awk -v num=3 'BEGIN {var=gensub(/ /,",","g",sprintf("%*s",num,"")); print var}'
,,,
$
$ awk -v num=0 'BEGIN {var=gensub(/ /,",","g",sprintf("%*s",num,"")); print var}'

$
Ed Morton
  • 188,023
  • 17
  • 78
  • 185