如何在linux中获得两个文件之间的差异(仅添加)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15384818/
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 get the difference (only additions) between two files in linux
提问by user1004985
I have two files A1 and A2 (unsorted). A1 is previous version of A2 and some lines have been added to A2. How can I get the new lines that are added to A2?
我有两个文件 A1 和 A2(未排序)。A1 是 A2 的先前版本,并且在 A2 中添加了一些行。如何获得添加到 A2 的新行?
Note: I just want the new lines added and dont want the lines which were in A1 but deleted in A2. When i do diff A1 A2
, I get the additions as well as deletions but I want only additions.
注意:我只想添加新行,不想要 A1 中但在 A2 中删除的行。当我这样做时diff A1 A2
,我会得到添加和删除,但我只想要添加。
Please suggest a way to do this.
请提出一种方法来做到这一点。
采纳答案by timrau
diff
and then grep
for the edit type you want.
diff
然后grep
是您想要的编辑类型。
diff -u A1 A2 | grep -E "^\+"
回答by Mihai8
The simple method is to use :
简单的方法是使用:
sdiff A1 A2
Another method is to use comm
, as you can see in Comparing two unsorted lists in linux, listing the unique in the second file
另一种方法是使用comm
,正如您在比较linux中的两个未排序列表中看到的那样,在第二个文件中列出唯一的
回答by Zabador
You can type:
您可以输入:
grep -v -f A1 A2
回答by merp
You can try this
你可以试试这个
diff --changed-group-format='%>' --unchanged-group-format='' A1 A2
The options are documented in man diff
:
选项记录在man diff
:
--GTYPE-group-format=GFMT
format GTYPE input groups with GFMT
and:
和:
LTYPE is 'old', 'new', or 'unchanged'.
GTYPE is LTYPE or 'changed'.
and:
和:
GFMT (only) may contain:
%< lines from FILE1
%> lines from FILE2
[...]
回答by scottkosty
All of the below is copied directly from @TomOnTime's serverfault answer here:
所有的下面是直接从@ TomOnTime的serverfault答案抄在这里:
Show lines that only exist in file a: (i.e. what was deleted from a)
显示只存在于文件 a 中的行:(即从 a 中删除的内容)
comm -23 a b
Show lines that only exist in file b: (i.e. what was added to b)
显示仅存在于文件 b 中的行:(即添加到 b 中的内容)
comm -13 a b
Show lines that only exist in one file or the other: (but not both)
显示只存在于一个文件或另一个文件中的行:(但不能同时存在)
comm -3 a b | sed 's/^\t//'
(Warning: If file a
has lines that start with TAB, it (the first TAB) will be removed from the output.)
(警告:如果文件a
有以 TAB 开头的行,它(第一个 TAB)将从输出中删除。)
NOTE: Both files need to be sorted for "comm" to work properly. If they aren't already sorted, you should sort them:
注意:这两个文件都需要进行排序才能使“comm”正常工作。如果它们尚未排序,您应该对它们进行排序:
sort <a >a.sorted
sort <b >b.sorted
comm -12 a.sorted b.sorted
If the files are extremely long, this may be quite a burden as it requires an extra copy and therefore twice as much disk space.
如果文件非常长,这可能是一个很大的负担,因为它需要额外的副本,因此需要两倍的磁盘空间。
Edit: note that the command can be written more concisely using process substitution (thanks to @phk for the comment):
编辑:请注意,可以使用进程替换更简洁地编写命令(感谢@phk 的评论):
comm -12 <(sort < a) <(sort < b)
回答by user1046885
git diff path/file.css | grep -E "^\+" | grep -v '+++ b/' | cut -c 2-
grep -E "^\+"
is from previous accepted answer, it is incomplete because leaves non-source stuffgrep -v '+++ b'
removes non-source line with file name of later versioncut -c 2-
removes column of+
signs, also may usesed 's/^\+//'
grep -E "^\+"
来自之前接受的答案,它不完整,因为留下了非源代码grep -v '+++ b'
删除具有更高版本文件名的非源代码行cut -c 2-
删除+
标志列,也可以使用sed 's/^\+//'
comm
or sdiff
were not an option because of git.
comm
或者sdiff
因为 git 而不是一个选项。
回答by Francesc Rosas
A similar approach to merp's but hopefully more understandable and easy to tweak:
类似于merp的方法,但希望更容易理解和易于调整:
diff \
--new-line-format="- %L" \
--old-line-format="" \
--unchanged-line-format="" \
A1 A2