我该如何将多个参数传递给Unix命令?

7

我找不到任何相似的内容,所以来提问。

假设我有一组命令可以提供输出结果。

第一组命令会产生一个输出结果。

cat xyx.txt | awk '{.........}' | sed 's/.../' | cut -d....   
第二个集合会产生输出。
cat abc.txt | awk '{.........}' | cut -d ... | sed 's/...../'
我希望将这两个作为“join”命令的2个参数输出。我知道可以将这两个重定向到一个文件中,然后使用该文件作为参数运行“join”命令。 基本上,是否可以在一行代码中完成整个操作…像这样:
[first set of commands] > join -1 1 -2 1 < [second set of commands] 

1
我猜你应该在这里发布:http://unix.stackexchange.com/ - Ionică Bizău
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
9

如果您正在使用bash,这是许多Linux发行版中的默认shell,则表达式:

join <([first set of commands]) <([second set of commands])

有效。

所以

join <(cat xyx.txt | awk '{.........}' | sed 's/.../' | cut -d....) <(cat abc.txt | awk '{.........}' | cut -d ... | sed 's/...../')

should make it.

Basic example

$ cat a
1 hello
2 bye
3 ciao
$ cat b
1 hello 123
2 bye 456
3 adios 789
$ cut -d' ' -f1,2 b | awk '{print $1, 2, $2}'
1 2 hello
2 2 bye
3 2 adios
$ join <(cut -d' ' -f1,2 b | awk '{print $1, 2, $2}') a
1 2 hello hello
2 2 bye bye
3 2 adios ciao

太棒了!非常感谢,William。现在我需要做什么?我该如何关闭它并将其设置为由您回答? - megatron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,