4

I would like to use the linux cut command to extract a column from a file, then use the paste command to insert the same column into a second file. I can do this by saving the results of the cut command, and then running paste on it and the second file. But it seems to me there must be some one-liner for doing this that doesn't involve saving intermediate results. Anyone know what that is? Thanks.

For example, the first file might look like

date        weight
1-1-2010    weight1
1-2-2010    weight2
1-3-2010    weight3

and the second might look like

date        blood_press
1-1-2010    bp1
1-2-2010    bp2
1-3-2010    bp3

and I would like output like

date       weight     blood_press
1-1-2010   weight1    bp1
1-2-2010   weight2    bp2
1-3-2010   weight3    bp3

Needless to say, the data is alot larger and more complicated than this. But this gives the idea of what I need to do. Thanks again.

P.S. For reasons too detailed to go into, the "join" command isn't going to work.

4

1 回答 1

8

如果您可以使用制表符分隔符(或其他单个字符),您可以这样做

cut [column-spec] file1 | paste file2 - > file3

-命令中的读取paste标准输入,其中当然包含由cut. 值没有匹配,这是逐行复制和粘贴的直线。

于 2013-07-23T17:31:44.007 回答