14

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?

3
  • You know about diff , right? Commented Jul 15, 2014 at 11:26
  • @schaiba Thank you. Just came across that on another forum, works great. I hadn't stumbled upon it before that. I hope this answer is useful for future queries by other members. Commented Jul 15, 2014 at 11:28
  • @schaiba may as well make an answer of it. Make sure to deal with diff working on lines not fields though. Commented Jul 15, 2014 at 13:07

6 Answers 6

18

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.

2
  • 1
    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... Commented Dec 29, 2015 at 11:09
  • 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 Commented Apr 30, 2021 at 9:43
11

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

1
7

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

vim -d file1 file2
3

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.

2

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

0

Go for vimdiff for Showing differences between two, three or four versions of the same file.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.