我正在尝试连接两个具有不同行但有一个共同元素的文件。
文件1:
1/temperature
2/rainfall
3/snowfall
4/windspeed
5/winddirection
文件2:
2008-10-01/1/68
2008-10-02/2/6
2008-10-03/3/4
2008-10-04/4/25
2008-10-05/5/120
2008-10-01/1/89
2008-10-01/4/35
2008-11-01/5/360
2008-10-01/1/45
2008-12-01/1/61
2008-10-10/1/32
我正在尝试使用以下命令加入两个文件
join -t/ -1 1 -2 2 FILE1 FILE2
我期望以下输出:
1/temperature/2008-10-01/68
1/temperature/2008-10-01/89
1/temperature/2008-10-01/45
1/temperature/2008-12-01/61
1/temperature/2008-10-10/32
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
4/windspeed/2008-10-01/35
5/winddirection/2008-10-05/120
5/winddirection/2008-11-01/360
但我的前 5 行是这样的:
1/temperature/2008-10-01/68
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
5/winddirection/2008-10-05/120
请你帮助我做错了什么
答案1
join
期望对其输入进行排序(在要连接的字段上)。在本例中(利用 bash shell 的进程替换):
$ join -t/ -1 1 -2 2 FILE1 <(sort -t/ -k2 FILE2)
1/temperature/2008-10-10/32
1/temperature/2008-10-01/45
1/temperature/2008-12-01/61
1/temperature/2008-10-01/68
1/temperature/2008-10-01/89
2/rainfall/2008-10-02/6
3/snowfall/2008-10-03/4
4/windspeed/2008-10-04/25
4/windspeed/2008-10-01/35
5/winddirection/2008-10-05/120
5/winddirection/2008-11-01/360
请注意,所需的排序是词汇的而不是数字 - 如果第一列值FILE1
超过 9,那么您可能还需要对其进行预排序。