像在 SAS 中一样加入 bash [英] join in bash like in SAS

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

问题描述

我想在 bash 中使用一个公共列连接两个文件.我想保留两个文件中所有可配对和不可配对的行.不幸的是,使用 join 我只能从一个文件中保存不可配对的字段,例如.加入 -1 1 -2 2 -a1 -t" ".
我还想保留两个文件中重复条目(在连接列中)的所有配对.IE.如果 file1 是
x id1 a b
x id1 c d
x id1 d f
x id2 c x
x id3 f v

I'd like to join two files in bash using a common column. I want to retain both all pairable and unpairable lines from both files. Unfortunately using join I could save unpairable fields from only one file, eg. join -1 1 -2 2 -a1 -t" ".
I'd also want to retain all pairings for repeated entries (in join column) from both files. I.e. If file1 is
x id1 a b
x id1 c d
x id1 d f
x id2 c x
x id3 f v

第二个文件是

id1 df cf
id1 ds dg
id2 cv df
id2 as ds
id3 cf cg

id1 df cf
id1 ds dg
id2 cv df
id2 as ds
id3 cf cg

生成的文件应该是:

the resulting file should be:

x id1 a b df cf
x id1 a b ds dg
x id1 c d df cf
x id1 c d ds dg
x id1 d f df cf
x id1 d f ds dg
x id2 c x cv df
x id2 c x as ds
x id3 f v cf cg

x id1 a b df cf
x id1 a b ds dg
x id1 c d df cf
x id1 c d ds dg
x id1 d f df cf
x id1 d f ds dg
x id2 c x cv df
x id2 c x as ds
x id3 f v cf cg

这就是为什么我总是使用 SAS 在对适当的列进行排序后进行此类连接.

That's why I've always using SAS to make such join, after sorting appropriate columns.

数据 x;
合并file1 file2;
按 common_column;
运行;

它工作正常,但
1. 因为我大部分时间都使用 Ubuntu,所以我必须切换到 Windows 来合并 SAS 中的数据.
2.最重要的是,SAS 可以截断过长的数据条目.

It works fine but
1. as I use Ubuntu for most time I have to switch to Windows to merge data in SAS.
2. most importantly, SAS can truncate too long data entries.

这就是为什么我更喜欢在 bash 中加入我的文件,但我不知道合适的命令.
有人可以帮助我,或指导我找到适当的资源吗?

That's why I'd prefer to join my files in bash, but I don't know appropriate command.
Could someone help me, or direct me to appropriate resource?

推荐答案

根据join的手册页,-a <filenum>会保留所有无法配对的行文件 (1 或 2).因此,只需将 -a1 -a2 添加到您的命令行,您就应该完成了.例如:

According to join's man page, -a <filenum> retains all unpairable lines from file <filenum> (1 or 2). So, just add -a1 -a2 to your command line and you should be done. For example:

# cat a
1 blah
2 foo

# cat b
2 bar
3 baz

# join -1 1 -2 1 -t" " a b
2 foo bar

# join -1 1 -2 1 -t" " -a1 a b
1 blah
2 foo bar

# join -1 1 -2 1 -t" " -a2 a b
2 foo bar
3 baz

# join -1 1 -2 1 -t" " -a1 -a2 a b
1 blah
2 foo bar
3 baz

这就是你要找的吗?

由于您提供了更多详细信息,以下是生成所需输出的方法(请注意,我的文件 a 是您的第一个文件,我的文件 b 是您的第二个文件.我必须将 -1 1 -2 2 反转为 -1 2 -2 1 才能加入 id).我还添加了一个字段列表来格式化输出 - 请注意,0"是其中的连接字段:

Since you provided more detail, here is how to produce your desired output (note that my file a is your first file and my file b your second file. I had to reverse -1 1 -2 2 to -1 2 -2 1 to join on the id). I added a field list to format the output as well - note that '0' is the join field in it:

# join -1 2 -2 1 -o 1.1,0,1.3,1.4,2.2,2.3 a b

产生你所给予的.添加 -a1 -a2 以保留两个文件中不可配对的行,然后再获得两行(您可以从中猜出我的测试数据):

produces what you've given. Add -a1 -a2 to retain unpairable lines from both files you then get two more lines (you can guess my test data from them):

x id4 u t
 id5   ui oi

这是相当不可读的,因为任何遗漏的字段都只是一个空格.所以让我们用'-'替换它们,导致:

Which is rather unreadable since any left out field is just a space. So let's replace them with a '-', leading to:

# join -1 2 -2 1 -a1 -a2 -e- -o 1.1,0,1.3,1.4,2.2,2.3 a b
x id1 a b df cf
x id1 a b ds dg
x id1 c d df cf
x id1 c d ds dg
x id1 d f df cf
x id1 d f ds dg
x id2 c x cv df
x id2 c x as ds
x id3 f v cf cg
x id4 u t - -
- id5 - - ui oi

这篇关于像在 SAS 中一样加入 bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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