Comparing two files in linux terminal

后端 未结 10 1135
一生所求
一生所求 2020-12-22 17:31

There are two files called \"a.txt\" and \"b.txt\" both have a list of words. Now I want to check which words are extra in \"a.txt\

相关标签:
10条回答
  • 2020-12-22 18:00

    Here is my solution for this :

    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
    
    0 讨论(0)
  • 2020-12-22 18:05

    Try sdiff (man sdiff)

    sdiff -s file1 file2
    
    0 讨论(0)
  • 2020-12-22 18:06

    You can use diff tool in linux to compare two files. You can use --changed-group-format and --unchanged-group-format options to filter required data.

    Following three options can use to select the relevant group for each option:

    • '%<' get lines from FILE1

    • '%>' get lines from FILE2

    • '' (empty string) for removing lines from both files.

    E.g: 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
    
    0 讨论(0)
  • 2020-12-22 18:07

    Also, do not forget about mcdiff - Internal diff viewer of GNU Midnight Commander.

    For example:

    mcdiff file1 file2
    

    Enjoy!

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