How do I do a one way diff in Linux?

本秂侑毒 提交于 2019-12-01 05:33:18

diff A B|grep '^<'|awk '{print $2}'

grep '^<' means select rows start with <

awk '{print $2}' means select the second column

An alternative, if your files consist of single-line entities only, and the output order doesn't matter (the question as worded is unclear on this), would be:

comm -23 <(sort A) <(sort B)

comm requires its inputs to be sorted, and the -2 means "don't show me the lines that are unique to the second file", while -3 means "don't show me the lines that are common between the two files".

If you need the "differences" to be presented in the order they occur, though, the above diff / awk solution is ok (although the grep bit isn't really necessary - it could be diff A B | awk '/^</ { $1 = ""; print }'.

EDIT: fixed which set of lines to report - I read it backwards originally...

As stated in the comments, one mostly correct answer is

diff A B | grep '^<'

although this would give the output

< good dog
< two

rather than

2c2
< good dog
4c4,5
< two
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!