Exclude a column when pasting two data files

别说谁变了你拦得住时间么 提交于 2019-12-25 10:36:04

问题


I have one file "dat1.txt" which is like:

0 5.71159e-01
1 1.92632e-01
2 -4.73603e-01

and another file "dat2.txt" which is:

0 5.19105e-01
1 2.29702e-01
2 -3.05675e-01

to write combine these two files into one I use

paste dat1.txt dat2.txt > data.txt

But I do not want the 1st column of the 2nd file in the output file. How do I modify the unix command?


回答1:


paste  dat1.txt <(cut -d" " -f2- dat2.txt)

Using cut to remove column 1, and using process substitution to use its output in paste

Output:

0 5.71159e-01   5.19105e-01
1 1.92632e-01   2.29702e-01
2 -4.73603e-01  -3.05675e-01



回答2:


If your files are in sorted order along column 1, you could try:

join dat[12].txt



回答3:


You could try this in awk itself,

$ awk 'FNR==NR {a[FNR]=$0;next} {print a[FNR],$2}' data1.txt data2.txt
0 5.71159e-01 5.19105e-01
1 1.92632e-01 2.29702e-01
2 -4.73603e-01 -3.05675e-01



回答4:


Use cut to remove the first column and then pipe to paste.

 cut -d' ' -f 1 --complement dat2.txt  | paste dat1.txt - > data.txt

Note that the - in the past ecommand means to read from stdin in place of the second file.

If cut is broken on OSX, awk might work.

 awk '{for (i=2; i<=NF; i++) print $i}'  dat2.txt  | paste dat1.txt - > data.txt


来源:https://stackoverflow.com/questions/23977834/exclude-a-column-when-pasting-two-data-files

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