1

I have four text files each containing a single field. I want to convert them into a single text file with four fields (one from each file). How can I do this using shell scripting.

2 Answers 2

3

This should do:

paste file_1 file_2 file_3 file_4

If you want pretty printing (given that the number of lines are same for all the files), then:

paste 1 2 3 4 | column -t
4
  • note that column is likely to break things for files that don't have the same number of lines.
    – peterph
    Commented Feb 3, 2015 at 19:49
  • @peterph: yes, edited.
    – heemayl
    Commented Feb 3, 2015 at 19:50
  • @peterph - depends what switches his/her version of column accepts... see glenn jackman's answer here - it works fine with files that don't have the same number of lines. Commented Feb 3, 2015 at 21:11
  • @don_crissti "... is likely to break things..." You'll have to fiddle with it to make sure things will be what you want them to be.
    – peterph
    Commented Feb 4, 2015 at 13:38
2

The paste command does exactly what you're looking for, and the good news is that it's POSIX-standard, so it's available effectively everywhere.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .