Shell 带awk(或sed)的comm命令
我想使用Shell 带awk(或sed)的comm命令,shell,awk,sed,conemu,Shell,Awk,Sed,Conemu,我想使用comm打印出两个输入流中仅一个流中存在的所有行(但此命令的使用不是强制性的)。然而,使用awk或sed似乎有点混乱。例如,comm-23(git branch-r | awk-F\“[/]\”/matchinstring/{print$2}”排序)(blah blah other stream)失败(“系统找不到指定的文件”)awk尝试使用|排序…部分。有没有办法克服这个问题?当然,我可以输出两个文件,然后在这两个文件上使用comm,但我可以将其设置为一行吗?我用的是ConEmu 示例
comm
打印出两个输入流中仅一个流中存在的所有行(但此命令的使用不是强制性的)。然而,使用awk
或sed
似乎有点混乱。例如,comm-23(git branch-r | awk-F\“[/]\”/matchinstring/{print$2}”排序)(blah blah other stream)
失败(“系统找不到指定的文件”)<代码>awk尝试使用|排序…
部分。有没有办法克服这个问题?当然,我可以输出两个文件,然后在这两个文件上使用comm
,但我可以将其设置为一行吗?我用的是ConEmu
示例输入1:
matchingstring-234
matchingstring-456
示例输入2:
matchingstring-123
matchingstring-345
matchingstring-456
预期产出:
matchingstring-234
由于您使用的是bash(基于标记),因此可以使用:
comm-23输入文件:
$ cat f1
matchingstring-234
matchingstring-456
$ cat f2
matchingstring-123
matchingstring-345
matchingstring-456
使用awk
$ awk 'FNR==NR{arr[$1];next}!( $1 in arr)' f2 f1
matchingstring-234
使用grep
$ grep -v -F -x -f f2 f1
matchingstring-234
使用join
$ join -v 2 <(sort f2) <(sort f1)
matchingstring-234
答案取决于你使用的外壳
正如在bash和ksh(93)中提到的@HardcoreHenry和@shelleter一样,您可以使用comm-23从弹出窗口中选择一个shell。或“功能表>设置>启动>指定的命名任务”(这将设置ConEmu默认启动的外壳)。请将示例输入文件和预期输出文件也张贴在帖子的代码标记中。所以我们可以帮你做同样的事情。关键是我们没有输入输出文件。git将生成一个字符串流。另外(blah blah other stream)也是git生成的字符串流。但是我用这些流的样子来编辑我的帖子,明白了,但是如果你能展示你第一个命令的输出和预期的输出,我们在这里会更容易提供帮助。问题与ConEmu无关,而只是你正在使用的shell!我明白了,所以外壳可能是个问题。我必须使用windows版本,在本例中是ConEmu-Maximus5。我试过你的建议,但显然conEmu不喜欢。啊,是的,这是bash(或zsh)特有的。既然你用bash标记了你的问题,我想你是在用它。您可以查看,但我不确定这是否比使用文件更可读……好吧,在本例中,我将bash标记更改为conemu。显然,conemu不是bash。但是您可以在ConEmu中运行bash,并且ConEmu不会禁止您在bash提示符中运行建议的命令(也就是说,如果您有一个包含a
a
b
的文件,以及另一个a
b
的文件,这将无法检测到额外的a
行。(不过,OP引用的git分支
可能不会输出重复行)问题不是如何获得这两个集合的差异(因为comm-23很好)。正如我提到的,我想使用不带文件的命令。您的所有解决方案都使用文件作为输入。
$ grep -v -F -x -f f2 f1
matchingstring-234
$ join -v 2 <(sort f2) <(sort f1)
matchingstring-234
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular
expressions), separated by newlines, any of which is to be
matched.
-x, --line-regexp
Select only those matches that exactly match the whole line.
For a regular expression pattern, this is like parenthesizing
the pattern and then surrounding it with ^ and $.
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. Multiple
-f can be used to specify different files.