Ali*_*ran 155 linux terminal diff file-comparison
有两个名为"a.txt"和"b.txt"的文件都有一个单词列表.现在我想检查"a.txt"中哪些单词是额外的,而不是"b.txt".
我需要一个有效的算法,因为我需要比较两个词典.
Fen*_* Li 326
如果你安装了vim,试试这个:
vimdiff file1 file2
Run Code Online (Sandbox Code Playgroud)
要么
vim -d file1 file2
Run Code Online (Sandbox Code Playgroud)
你会发现它太棒了.
And*_*son 68
对它们进行排序和使用comm:
comm -23 <(sort a.txt) <(sort b.txt)
Run Code Online (Sandbox Code Playgroud)
comm比较(排序)输入文件,默认输出三列:a是唯一的行,b是唯一的行,以及两者中都有的行.通过指定-1,-2和/或-3可以抑制相应的输出.因此,comm -23 a b仅列出a的唯一条目.我使用<(...)语法动态地对文件进行排序,如果它们已经排序,则不需要这样.
Man*_*ula 27
您可以使用difflinux中的工具来比较两个文件.您可以使用--changed-group-format和--unchanged-group-format选项来过滤所需的数据.
以下三个选项可用于为每个选项选择相关组:
'%<'从FILE1获取行
'%>'从FILE2获取行
''(空字符串)用于从两个文件中删除行.
例如:diff --changed-group-format ="%<" - unchanged-group-format =""file1.txt file2.txt
[root@vmoracle11 tmp]# cat file1.txt
test one
test two
test three
test four
test eight
[root@vmoracle11 tmp]# cat file2.txt
test one
test three
test nine
[root@vmoracle11 tmp]# diff --changed-group-format='%<' --unchanged-group-format='' file1.txt file2.txt
test two
test four
test eight
Run Code Online (Sandbox Code Playgroud)
joe*_*lom 21
如果您更喜欢diff输出样式git diff,可以将它与--no-index标志一起使用来比较不在git存储库中的文件:
git diff --no-index a.txt b.txt
Run Code Online (Sandbox Code Playgroud)
使用几个文件中包含大约200k文件名字符串的文件,我使用time这种方法(使用内置命令)对比其他一些答案:
git diff --no-index a.txt b.txt
# ~1.2s
comm -23 <(sort a.txt) <(sort b.txt)
# ~0.2s
diff a.txt b.txt
# ~2.6s
sdiff a.txt b.txt
# ~2.7s
vimdiff a.txt b.txt
# ~3.2s
Run Code Online (Sandbox Code Playgroud)
comm似乎是迄今为止最快的,但git diff --no-index似乎是差异式输出的最快方法.
更新2018-03-25--no-index除非您在git存储库中并想要比较该存储库中未跟踪的文件,否则您实际上可以省略该标志.从手册页:
此表单用于比较文件系统上给定的两个路径.在由Git控制的工作树中运行命令时,可以省略--no-index选项,并且至少有一个路径指向工作树外部,或者在Git控制的工作树外运行命令时.
小智 9
您还可以使用:colordiff:使用颜色显示diff的输出.
关于vimdiff:它允许您通过SSH比较文件,例如:
vimdiff /var/log/secure scp://192.168.1.25/var/log/secure
Run Code Online (Sandbox Code Playgroud)
摘自:http://www.sysadmit.com/2016/05/linux-diferencias-entre-dos-archivos.html
另外,不要忘记mcdiff - GNU Midnight Commander的内部差异查看器.
例如:
mcdiff file1 file2
Run Code Online (Sandbox Code Playgroud)
请享用!
这是我的解决方案:
mkdir temp
mkdir results
cp /usr/share/dict/american-english ~/temp/american-english-dictionary
cp /usr/share/dict/british-english ~/temp/british-english-dictionary
cat ~/temp/american-english-dictionary | wc -l > ~/results/count-american-english-dictionary
cat ~/temp/british-english-dictionary | wc -l > ~/results/count-british-english-dictionary
grep -Fxf ~/temp/american-english-dictionary ~/temp/british-english-dictionary > ~/results/common-english
grep -Fxvf ~/results/common-english ~/temp/american-english-dictionary > ~/results/unique-american-english
grep -Fxvf ~/results/common-english ~/temp/british-english-dictionary > ~/results/unique-british-english
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
276579 次 |
| 最近记录: |