I have 58 files named as xaa.csv to xcf.csv, none of them have headers. I have a separate file with the header. How can I merge the files with the header and have 59 outputs merged?

For example:

input xaa.csv xab.csv ..... xcf.csv
xaa + header.csv xab + header.csv....
  • It seems that DopeGhoti created a solution that results in 59 files, each with a header; did you want one file with (header + 58 inputs), or 59 files with (header + 1 input) ? – Jeff Schaller Jun 20 '17 at 17:05
  • No, I wanted what dopeghoti said! Thank you, though! :) – caiolasagno' Jun 20 '17 at 17:57
  • perfect; don't forget to accept their answer, then! – Jeff Schaller Jun 20 '17 at 18:01
  • Hello Jeff, it's my first post here. How do I do it? Saying it was useful? – caiolasagno' Jun 20 '17 at 20:03
  • there should be a checkmark next to the answer; clicking it will make it turn green - that's it! – Jeff Schaller Jun 20 '17 at 20:29
for file in ???.csv; do
   cat header.csv "$file" > "joined-$file"
| improve this answer | |
  • I think he wants header after the contents. – unxnut Jun 20 '17 at 16:48
  • I want header before the contents in each file – caiolasagno' Jun 20 '17 at 16:50
  • The provided command will put the header ahead of the data; I figured that was what you wanted, or you would have called it a footer. – DopeGhoti Jun 20 '17 at 16:52
  • I wrote what you proposed DopeGhoti and saved as an .sh but it didn't seem to work. However, a friends proposed this: ls -l x*.csv | awk -F' ' '{body = $9; system("cat header.csv " body " > " body ".new")}' and it worked quite well! – caiolasagno' Jun 20 '17 at 17:18

With brace expansion:

cat header.csv x{a..b}{a..z}.csv xc{a..f}.csv > merged-file

This puts the contents of header.csv, followed by the contents of each of the x??.csv files into one merged-file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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