How can I output the difference between 2 files?

115,064

Solution 1

The utility you're looking for is diff. Take a peek at the manual for details.

Solution 2

Given two files containing unsorted lists of users, e.g.

In file1:
    userD
    user3
    userA
    user1
    userB

and

In file2:
    user3
    userB
    userX
    user1

then to get a simple list of the users in file1 but not in file2, you can do

$ comm -23 <(sort file1) <(sort file2)
userA
userD

and similarly to get the users in file2 but not in file1

$ comm -13 <(sort file1) <(sort file2)
userX

If the list files are already sorted, these can be simplified to comm -23 file1 file2 and comm -13 file1 file2 respectively.

Solution 3

The best command to view the difference in the files content would be

vim -d file1 file2

Solution 4

diff [options] from-file to-file

diff compares the contents of the two files from-file and to-file.You can specify the -i option that ignores changes in case; consider upper- and lower-case letters equivalent.

For more information you can refer this link : http://www.computerhope.com/unix/udiff.htm or you can have a look at the manual page.

Solution 5

you can try the same windows like fc command in Unix and Linux i.e. diff <file_new> <file_old> command.

Line with +++ or --- in front of them have changed and one with no +'s and -'s haven't changed

Lines with - sign are removed from the new file however they existed in old version

Lines with + sign are added from in new file however they didn't existed in old version of the file

Linux notation

Linux uses > and <

> means the line from old file < means the line from new file that is changed in old file

Share:
115,064

Related videos on Youtube

Ben
Author by

Ben

Updated on September 18, 2022

Comments

  • Ben
    Ben over 1 year

    My file consists of the the following; roughly:

    username:username:username:username:username 
    

    The above line continues to about 600 characters.

    I use the awk command in order to use it as an argument in a API/HTTP request sent from the command line.

    I'm using my script to get a list of user accounts 'following' me, and every 24 hours or so, comparing the original list on my hard disk to the newly outputted username list (and echo'ing out who is no longer following me. I will have to encapsulate my logic into a loop using bash.. testing each username.

    My current script:

    user=$(awk -F: '{ print $1 }' FILE)  # Grab $User to use as an argument.  
    following=$(exec CURRENT_FOLLOWERS) # Outputs the new file
    
    echo "X amount of users are following you on 78B066B87AF16A412556458AC85EFEF66155"  
    
              SAVE CURRENT FOLLOWERS TO NEW A FILE.  
    
    
    if [[ DIFFERENCE IS DETECTED ]] ; then    
    
              echo -ne "$User NO LONGER FOLLOWING YOU\r"
    
       else echo -ne "This user is following you still.\r"
    fi
    

    My question is;
    How can I output the difference between 2 files?

    • schaiba
      schaiba almost 10 years
      You know about diff , right?
  • Lanti
    Lanti over 8 years
    Is it possible to use comm without sorting? My text file has values like 1, 2, 01, 02, 0000, 0001, 0002, etc. Using sort will de-sort the order...
  • rand
    rand almost 8 years
  • bcag2
    bcag2 about 3 years
    comm -23 file1 file2 works for me ! It is in my case on files list (pictures and thumb) and I run comm -23 img_list thumb_list > img-ok_th-nok and I have thumb list I should generate