简体   繁体   English

没有临时文件的两个外壳程序列表之间的交集

[英]Intersection between two shell lists without temp files

Lots of questions has been put on the web about the result that I want. 关于我想要的结果,网上有很多问题。 Imagine, you have got two lists in a shell script: 想象一下,您在shell脚本中有两个列表:

 LIST_ONE="key1 key2 key3 key4"
 LIST_TWO="key2 key3 key5"

I would like the intersection of this two lists: 我想要这两个列表的交集:

 LIST_FIN="key2 key3"

I found some things to do like using comm command. 我发现有一些事情要做,例如使用comm命令。 But it always uses files, and I can't use temp files. 但是它总是使用文件,而我不能使用临时文件。

Thanks for your advice. 谢谢你的建议。

Use process substitution: 使用流程替代:

grep -o -f <(echo "$LIST_TWO" | tr ' ' '\n') <<< "$LIST_ONE" | xargs
key2 key3

How about something like this: 这样的事情怎么样:

$ LIST_ONE="key1 key2 key3 key4"
$ LIST_TWO="key2 key3 key5"
$ comm -12 <(tr ' ' '\n' <<< "${LIST_ONE}" | sort) <(tr ' ' '\n' <<< "${LIST_TWO}" | sort)
key2
key3
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM