加入在Linux上的两个文件 [英] Join two files on Linux

查看:159
本文介绍了加入在Linux上的两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件;我想加入他们的行列。

I have two files; I want to join them.

$cat t1
 1 1.2
 2 2.2
$cat t2
 1
 2
 1

我想有以下

$cat joind.txt
 1 1.2
 2 2.2
 1 1.2

但是当我使用加入命令,第三行不会出现在输出中。

but when I use the join command, the third line does not appear in the output.

推荐答案

加入 的需要这两个文件进行排序。如果先对它们进行排序,你会得到所有的输出

join requires that both files to be sorted. If you sort them first, you'll get all your output

$ sort t1 > t1.sorted
$ sort t2 > t2.sorted
$ join -j1 -o 1.1,1.2 t1.sorted t2.sorted
1 1.2
1 1.2
2 2.2

没有排序:

$ join -j1 -o 1.1,1.2 t1 t2
1 1.2
2 2.2

这假定你输入的顺序并不需要是preserved;如果他们这样做,你将需要自定义脚本像其他的答案提供了。

This assumes that the order of your inputs don't need to be preserved; if they do, you will need a custom script like other answers have provided.

这篇关于加入在Linux上的两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆