24

This question already has an answer here:

I have two files

File1:

a
b
c

File2:

1
2
3

now I need to combine them to one csv file

a;1
b;2
c;3

As the files are really huge, I would rather not use cat and sed to process the second file. (For smaller files I can use a script). Any Idea ? awk/perl ?

marked as duplicate by don_crissti, Jeff Schaller, Archemar, dr01, garethTheRed May 11 '16 at 11:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Nitpick - CSV is Comma Separated Values. I suspect many CSV utilities would reject semicolon separated values, at least with default settings. – Digital Trauma May 10 '16 at 20:24
  • 2
    @DigitalTrauma many utilities which are aware of locale will take semicolon separated values for certain locales, and reject the comma separated ones. – rumtscho May 10 '16 at 22:39
  • 2
    @DigitalTrauma According to Wikipedia, "CSV" is commonly used to refer to files with values separated by other delimiters too. It's quite confusing. – Anko May 11 '16 at 11:21
  • @Anko thanks - I did not know that :) – Digital Trauma May 11 '16 at 19:30
43

Try paste command :

paste -d';' File1 File2 > File3
2
#!/usr/bin/awk -f
{
  w[FNR][FILENAME] = $0
}
END {
  for (x in w)
    print w[x][ARGV[1]] ";" w[x][ARGV[2]]
}

I also came up with this, but it fails because of the way Awk iterates arrays:

#!/usr/bin/awk -f
{
  w[FNR][FILENAME] = $0
}
END {
  for (x in w) {
    y = 0
    for (z in w[x]) {
      printf w[x][z]
      printf y ? "\n" : ";"
      y = 1
    }
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.