How to paste columns from separate files using bash?

怎甘沉沦 提交于 2019-11-30 05:38:30

You were on track with paste(1):

$ paste -d , date1.csv date2.csv 
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

It's a bit unclear from your question if there are leading spaces on those lines. If you want to get rid of that in the final output, you can use cut(1) to snip it off before pasting:

 $ cut -c 2- date2.csv | paste -d , date1.csv -
  Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
  James,2013-06-03T17:18:07,2012-12-02T18:28:37
  Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

Another way of doing it is with pr

pr -mts, file1 file2

Test:

[jaypal:~/Temp] cat file1
Bob,2013-06-03T17:18:07
James,2013-06-03T17:18:07
Kevin,2013-06-03T17:18:07

[jaypal:~/Temp] cat file2
2012-12-02T18:30:31
2012-12-02T18:28:37
2013-06-01T12:16:05

[jaypal:~/Temp] pr -mts, file1 file2
Bob,2013-06-03T17:18:07,2012-12-02T18:30:31
James,2013-06-03T17:18:07,2012-12-02T18:28:37
Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

I wanted to extend jaypal's solution as I've ran into a need to edit files prior to merging the columns.

$cat date1.csv
 Bob,2013-06-03T17:18:07
 James,2013-06-03T17:18:07
 Kevin,2013-06-03T17:18:07

$cat date2.csv
 2012-12-02T18:30:31
 2012-12-02T18:28:37
 2013-06-01T12:16:05

Merging column 1 from date1.csv with column 1 from date2.csv can be accomplished as follows:

$pr -mts, <(cut -d, -f1 date1.csv) date2.csv
 Bob,2012-12-02T18:30:31
 James,2012-12-02T18:28:37
 Kevin,2013-06-01T12:16:05

You can apply further edits with a pipe if desired:

$pr -mts, <(cut -d, -f1 date1.csv | sort) date2.csv

Anyway, this has been handy for me and just wanted pass along the knowledge. Hope it helps someone.

If you just want to paste specific columns of different files side-by-side, you can use a combination of paste and cut.

For example, if you have three files with the same lines, only differing in some columns that you want to bring together:

$ head file1.csv
chr1H   1   240 RLC 2   138 239 0.5774059
chr1H   641 1787    RLC 12  1135    1146    0.9904014
chr1H   2009    3436    RLC 15  1413    1427    0.9901892
chr1H   4935    6106    RLG 12  1060    1171    0.9052092
chr1H   11523   11997   RLG 4   371 474 0.7827004
chr1H   11998   12882   RLX 9   776 884 0.8778281
chr1H   20340   21529   RLC 13  1177    1189    0.9899075
chr1H   27889   36240   RLC 82  8118    8351    0.9720991
chr1H   36241   39978   RLC 36  3542    3737    0.9478191
chr1H   40384   41273   RLX 10  880 889 0.9898763

$ head file2.csv
chr1H   1   240 RLC 1   39  239 0.1631799
chr1H   641 1787    RLC 11  1049    1146    0.9153578
chr1H   2009    3436    RLC 6   594 1427    0.4162579
chr1H   4935    6106    RLG 11  995 1171    0.8497011
chr1H   11523   11997   RLG 3   275 474 0.5801688
chr1H   11998   12882   RLX 4   378 884 0.4276018
chr1H   20340   21529   RLC 11  979 1189    0.8233810
chr1H   27889   36240   RLC 74  7238    8351    0.8667225
chr1H   36241   39978   RLC 31  3047    3737    0.8153599
chr1H   40384   41273   RLX 10  880 889 0.9898763

$ head file3.csv
chr1H   1   240 RLC 2   138 239 0.5774059
chr1H   641 1787    RLC 12  1135    1146    0.9904014
chr1H   2009    3436    RLC 15  1413    1427    0.9901892
chr1H   4935    6106    RLG 12  1060    1171    0.9052092
chr1H   11523   11997   RLG 4   371 474 0.7827004
chr1H   11998   12882   RLX 9   776 884 0.8778281
chr1H   20340   21529   RLC 13  1177    1189    0.9899075
chr1H   27889   36240   RLC 82  8118    8351    0.9720991
chr1H   36241   39978   RLC 36  3542    3737    0.9478191
chr1H   40384   41273   RLX 10  880 889 0.9898763

The first for columns of the files are identical. We want to keep these, but additionally paste the 8th column of each file side-by-side:

$ paste file1.csv file2.csv file3.csv | cut -f 1,2,3,4,8,16,24 | head

results in:

chr1H   1   240 RLC 0.5774059   0.1631799   0.0000000
chr1H   641 1787    RLC 0.9904014   0.9153578   0.6448517
chr1H   2009    3436    RLC 0.9901892   0.4162579   0.2081289
chr1H   4935    6106    RLG 0.9052092   0.8497011   0.1690862
chr1H   11523   11997   RLG 0.7827004   0.5801688   0.0000000
chr1H   11998   12882   RLX 0.8778281   0.4276018   0.1119910
chr1H   20340   21529   RLC 0.9899075   0.8233810   0.1068124
chr1H   27889   36240   RLC 0.9720991   0.8667225   0.4043827
chr1H   36241   39978   RLC 0.9478191   0.8153599   0.3914905
chr1H   40384   41273   RLX 0.9898763   0.9898763   0.3217098

This needs almost no memory and is probably as fast as it gets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!