Linux 如何使用差异(bash)仅显示不同的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10379795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display only different rows using diff (bash)
提问by frops
How can I display only different rows using diff in a separate file?
如何在单独的文件中使用 diff 仅显示不同的行?
For example, the file number 1 contains the line:
例如,文件编号 1 包含以下行:
1;john;125;3
1;tom;56;2
2;Hyman;10;5
A file number 2 contains the following lines:
文件编号 2 包含以下几行:
1;john;125;3
1;tom;58;2
2;Hyman;10;5
How to make in the following happen?
如何使以下情况发生?
1;tom;58;2
采纳答案by Anders Lindahl
a.txt:
.txt:
1;john;125;3
1;tom;56;2
2;Hyman;10;5
b.txt:
b.txt:
1;john;125;3
1;tom;58;2
2;Hyman;10;5
Use comm:
使用通讯:
comm -13 a.txt b.txt
1;tom;58;2
The command line options to comm
are pretty straight-forward:
命令行选项comm
非常简单:
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
-1 抑制第 1 列(FILE1 独有的行)
-2 抑制第 2 列(FILE2 独有的行)
-3 抑制第 3 列(出现在两个文件中的行)
回答by codaddict
Assuming you want to retain only the lines unique to file 2 you can do:
假设您只想保留文件 2 独有的行,您可以执行以下操作:
comm -13 file1 file2
Note that the comm
command expects the two files to be in sorted order.
请注意,该comm
命令要求两个文件按排序顺序排列。
回答by miroB
Using group format specifiers you can suppress printing of unchanged lines and print only changed lines for changed
使用组格式说明符,您可以禁止打印未更改的行并仅打印更改的行
diff --changed-group-format="%>" --unchanged-group-format="" file1 file2
diff --changed-group-format="%>" --unchanged-group-format="" file1 file2
回答by wisbucky
Here's a simple solution that I think is better than diff
:
这是我认为比diff
以下更好的简单解决方案:
sort file1 file2 | uniq -u
sort file1 file2 | uniq -u
sort file1 file2
concatenates the two files and sorts ituniq -u
prints the unique lines (that do not repeat). It requires the input to be pre-sorted.
sort file1 file2
连接两个文件并对其进行排序uniq -u
打印唯一的行(不重复)。它要求对输入进行预先排序。