How do I do a one way diff in Linux?

后端 未结 3 1522
渐次进展
渐次进展 2021-01-13 00:54

How do I do a one way diff in Linux?

Normal behavior of diff:

Normally, diff will tell you all the differences between a two files. For e

相关标签:
3条回答
  • 2021-01-13 01:14

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

    grep '^<' means select rows start with <

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

    0 讨论(0)
  • 2021-01-13 01:24

    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
    
    0 讨论(0)
  • 2021-01-13 01:27

    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...

    0 讨论(0)
提交回复
热议问题