Bash 中两个列表的交集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2696055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intersection of two lists in Bash
提问by User1
I'm trying to write a simple script that will list the contents found in two lists. To simplify, let's use ls as an example. Imagine "one" and "two" are directories.
我正在尝试编写一个简单的脚本来列出在两个列表中找到的内容。为简化起见,我们以 ls 为例。想象“一”和“二”是目录。
one=`ls one` two=`ls two` intersection $one $two
I'm still quite green in bash, so feel free to correct how I am doing this. I just need some command that will print out all files in "one" and "two". They must exist in both. You might call this the "intersection" between "one" and "two".
我对 bash 仍然很陌生,所以请随时纠正我的做法。我只需要一些命令来打印“一”和“二”中的所有文件。它们必须存在于两者中。您可以将其称为“一”和“二”之间的“交集”。
回答by ghostdog74
comm -12 <(ls 1) <(ls 2)
回答by Jean-Christophe Meillaud
Solution with comm
解决方案 comm
comm
is great but indeed need to work with sorted list. And fortunately here we use ls
which from ls
Bash man page
comm
很棒,但确实需要使用排序列表。幸运的是,我们在这里使用ls
了ls
Bash 手册页中的which
Sort entries alphabetically if none of -cftuSUX nor --sort.
如果没有 -cftuSUX 或 --sort,则按字母顺序对条目进行排序。
comm -12 <(ls one) <(ls two)
Alternative with sort
替代 sort
Intersection of two lists:
两个列表的交集:
sort <(ls one) <(ls two) | uniq -d
symmetric difference of two lists:
两个列表的对称差异:
sort <(ls one) <(ls two) | uniq -u
Bonus
奖金
Play with it ;)
玩它;)
cd $(mktemp -d) && mkdir {one,two} && touch {one,two}/file_{1,2}{0..9} && touch two/file_3{0..9}
回答by DVK
Use the comm
command:
使用comm
命令:
ls one | sort > /tmp/one_list
ls two | sort > /tmp/two_list
comm -12 /tmp/one_list /tmp/two_list
"sort" is not really needed but I always include it before using "comm" just in case.
“sort”并不是真正需要的,但我总是在使用“comm”之前包含它以防万一。
回答by Benubird
A less efficient (than comm) alternative:
效率较低(比通信)的替代方案:
cat <(ls 1 | sort -u) <(ls 2 | sort -u) | uniq -d
回答by frogstarr78
Join is another good option depending on the input and desired output
加入是另一个不错的选择,具体取决于输入和所需的输出
join -j1 -a1 <(ls 1) <(ls 2)
回答by Chuck Newman
There is another Stackoverflow question "Array intersection in bash," which is marked as a duplicate of this. It's not quite the same, in my opinion, as that question talks about comparing two bash arrays, while this question focuses on bash files. A one-line answer to the other question, which is now closed, is as follows:
还有另一个 Stackoverflow 问题“bash 中的数组交集”,它被标记为重复。在我看来,这并不完全相同,因为该问题讨论的是比较两个 bash 数组,而该问题则侧重于 bash 文件。对另一个问题的单行答案(现已关闭)如下:
# List1=( 0 1 2 3 4 6 7 8 9 10 11 12)
# List2=( 1 2 3 5 6 8 9 11 )
# List3=($(comm -12 <(echo ${List1[*]}| tr " " "\n"| sort) <(echo ${List2[*]} | tr " " "\n"| sort)| sort -g))
# echo ${List3[*]}
1 2 3 6 8 9 11
The comm utility does an alphanumeric sort, whereas the "Array intersection in bash" answers use numbers; hence the "sort" and "sort -g" usage.
comm 实用程序执行字母数字排序,而“bash 中的数组交集”答案使用数字;因此“排序”和“排序 -g”的用法。