Tuesday, December 9, 2014

How to paste and cut in one command

If you have two files that you want to paste side by side, use paste command.

file1:
chr1    11128   11519   MACS_peak_1     530.61
chr1    89268   89360   MACS_peak_2     58.63
chr1    153496  153625  MACS_peak_3     52.19
chr1    545624  545758  MACS_peak_4     63.70
chr1    564601  565447  MACS_peak_5     80.25

file2:
chr1    566050  566363  MACS_peak_6     120.67
chr1    567242  568254  MACS_peak_7     212.45
chr1    569057  570300  MACS_peak_8     169.08
chr1    704763  704818  MACS_peak_9     93.35
chr1    724126  724259  MACS_peak_10    58.44

paste file1 file2
chr1    11128   11519   MACS_peak_1     530.61  chr1    566050  566363  MACS_peak_6     120.67
chr1    89268   89360   MACS_peak_2     58.63   chr1    567242  568254  MACS_peak_7     212.45
chr1    153496  153625  MACS_peak_3     52.19   chr1    569057  570300  MACS_peak_8     169.08
chr1    545624  545758  MACS_peak_4     63.70   chr1    704763  704818  MACS_peak_9     93.35
chr1    564601  565447  MACS_peak_5     80.25   chr1    724126  724259  MACS_peak_10    58.44

If you want, in the same command, to cut the columns 1,2,3,5 from file2 and then do paste, making a pipe paste|cut doesn't work here:

paste file1 | cut -f1,2,3,5 file2
chr1    566050  566363  120.67
chr1    567242  568254  212.45
chr1    569057  570300  169.08
chr1    704763  704818  93.35
chr1    724126  724259  58.44

However, the command below works just fine:

paste file1 <(cut -f1,2,3,5 file2)
chr1    11128   11519   MACS_peak_1     530.61  chr1    566050  566363  120.67
chr1    89268   89360   MACS_peak_2     58.63   chr1    567242  568254  212.45
chr1    153496  153625  MACS_peak_3     52.19   chr1    569057  570300  169.08
chr1    545624  545758  MACS_peak_4     63.70   chr1    704763  704818  93.35
chr1    564601  565447  MACS_peak_5     80.25   chr1    724126  724259  58.44

So if you want to cut from both files and paste type:

paste <(cut -f1,2,3,5 file1) <(cut -f1,2,3,5 file2)
chr1    11128   11519   530.61  chr1    566050  566363  120.67
chr1    89268   89360   58.63   chr1    567242  568254  212.45
chr1    153496  153625  52.19   chr1    569057  570300  169.08
chr1    545624  545758  63.70   chr1    704763  704818  93.35
chr1    564601  565447  80.25   chr1    724126  724259  58.44

No comments:

Post a Comment