用于查找字符串集交集或异常值的Unix命令?

Evg*_*eny 20 unix grep set

是否有与UNIX相同的UNIX命令

sort | uniq
Run Code Online (Sandbox Code Playgroud)

找到字符串集交叉点或"异常值".

一个示例应用程序:我有一个html模板列表,其中一些有{%load i18n%}字符串在里面,有些则没有.我想知道哪些文件没有.

编辑: grep -L解决了上述问题.

这个怎么样:

文件1:

mom
dad
bob
Run Code Online (Sandbox Code Playgroud)

文件2:

dad
Run Code Online (Sandbox Code Playgroud)

%与file1 file2相交

dad
Run Code Online (Sandbox Code Playgroud)

%left-unique file1 file2

mom
bob
Run Code Online (Sandbox Code Playgroud)

Dal*_*und 37

它似乎grep -L解决了海报的真正问题,但对于实际问题,找到两组字符串的交集,你可能想查看"comm"命令.例如,if file1file2each包含一个排序的单词列表,每行一个单词

$ comm -12 file1 file2
Run Code Online (Sandbox Code Playgroud)

将生成两个文件共有的单词.更一般地说,给定排序的输入文件file1file2命令

$ comm file1 file2
Run Code Online (Sandbox Code Playgroud)

产生三列输出

  1. 仅在file1中的行
  2. 仅在file2中的行
  3. file1和file2中的行

您可以N使用-N选项禁止输出中的列.因此,上面的命令会comm -12 file1 file2抑制第1列和第2列,只留下两个文件共有的字.

  • 在比较之前,不要忘记通过排序运行文件.我做到了,结果让我陷入了疯狂的追逐. (3认同)

Joh*_*ica 9

相交:

# sort file1 file2 | uniq -d
dad
Run Code Online (Sandbox Code Playgroud)

左边唯一:

# sort file1 file2 | uniq -u
bob
mom
Run Code Online (Sandbox Code Playgroud)


its*_*dok 6

http://www.commandlinefu.com/commands/view/5710/intersection-between-two-files

两个(未排序)文件之间的交集:

grep -Fx -f file1 file2
Run Code Online (Sandbox Code Playgroud)

file2中不在file1中的行:

grep -Fxv -f file1 file2
Run Code Online (Sandbox Code Playgroud)

说明:

  • -f选项告诉grep读取要从文件中查找的模式。这意味着它将对file1中的每一行执行file2搜索。
  • -F选项告诉grep将搜索字词视为固定字符串,而不是模式,因此a.c只会匹配a.c而不是abc
  • -x选项告诉grep进行整行搜索,以便file1中的“ foo”与file2中的“ foobar”不匹配。
  • 默认情况下,grep将仅显示匹配的线,从而为您提供交点。该-v选项告诉grep仅显示不匹配的行,从而为file2提供唯一的行。


Tyl*_*nry 5

也许我误解了这个问题,但为什么不只是使用grep来查找字符串(使用-L选项让它打印出没有字符串的文件的名称).

换一种说法

grep -L "{% load i18n %}" file1 file2 file3 ... etc
Run Code Online (Sandbox Code Playgroud)

或者使用通配符作为文件名.