LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   get the difference between two files (https://www.linuxquestions.org/questions/linux-newbie-8/get-the-difference-between-two-files-903204/)

chesschi 09-15-2011 12:11 PM

get the difference between two files
 
Hi all,

I am fairly new to shell script. I want to get the difference for two files and only save the difference to another file, e.g.

File 1
Code:

12345
File 2
Code:

11111
22222
12345
33333
44444

I want the result becomes and save it to another file
Code:

11111
22222
33333
44444

If I use diff, there is '>' or '<' which I don't want.

What is the most efficient way to do this?

Thanks!!!

[ADDED]
Actually my purpose is to remove the SSH public key that I previously generated and appended to the authorized_keys file.
e.g.
Code:

cat keyfile1.pub >> /home/svnuser/.ssh/authorized_keys
cat keyfile2.pub >> /home/svnuser/.ssh/authorized_keys
cat keyfile3.pub >> /home/svnuser/.ssh/authorized_keys

Now I need to remove keyfile2.pub from authorized_keys file. So file 1 is keyfile2.pub and file 2 is authorized_keys.

TB0ne 09-15-2011 12:30 PM

Quote:

Originally Posted by chesschi (Post 4472640)
Hi all,
I am fairly new to shell script. I want to get the difference for two files and only save the difference to another file, e.g.
File 1
Code:

12345
File 2
Code:

11111
22222
12345
33333
44444

I want the result becomes and save it to another file
Code:

11111
22222
33333
44444

If I use diff, there is '>' or '<' which I don't want.
What is the most efficient way to do this?

You should look at grep. Read the man page, and pay particular attention to the "-f" and "-v" flags. For example, this:
Code:

grep -v -f File1 File2
produces the output you want. You can redirect it to another file from there. The "-f" is the flag that reproduces the deprecated fgrep command, for "file grep"

Aberrix 09-15-2011 01:12 PM

woops, nm just read you dont wanna use diff

mmrtnt 09-15-2011 02:11 PM

This Works
 
Code:

diff file1.txt file2.txt | sed -e 's/[<>]//g' | grep -v ','
It might be clumsy, but it gets the job done.

sed takes out the < and > and grep -v ',' removes any line with a comma.

frankbell 09-15-2011 07:54 PM

The unofficial leader of my LUG, who is pretty much a CLI kind of guy, was happy to find a GUI program called Meld which displays two scripts side-by-side, highlights the difference between them, and allows mouse-click transfer of highlighted items between files.

chesschi 09-16-2011 02:46 AM

Quote:

Originally Posted by TB0ne (Post 4472658)
Code:

grep -v -f File1 File2

My distro is CentOS 5.5. I get empty results on that.

Here is the -f option:
Code:

-f FILE, --file=FILE
      Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.


chesschi 09-16-2011 02:47 AM

Quote:

Originally Posted by Aberrix (Post 4472686)
woops, nm just read you dont wanna use diff

Sorry I am fairly new to Linux command. Please could you tell me the actual command (i.e. nm xxxx) I should type?

Thank you.

chesschi 09-16-2011 02:49 AM

Quote:

Originally Posted by mmrtnt (Post 4472731)
Code:

diff file1.txt file2.txt | sed -e 's/[<>]//g' | grep -v ','

It looks very close. But there is extra lines and '---'. Here is the result:

Code:

111
222

---
444
555


jschiwal 09-16-2011 02:56 AM

If the order of the lines is unimportant, look at the `comm' command. You need to sort the files. Then you can extract items unique to one of the files, or common to both.

comm -13 <(sort file1) <(sort file2) >uniq_in_file2

chesschi 09-16-2011 02:58 AM

Quote:

Originally Posted by frankbell (Post 4472984)
The unofficial leader of my LUG, who is pretty much a CLI kind of guy, was happy to find a GUI program called Meld which displays two scripts side-by-side, highlights the difference between them, and allows mouse-click transfer of highlighted items between files.

Thank you for your reply. Actually my purpose is to remove the SSH public key that I previously generated and appended to the authorized_keys file.
e.g.
Code:

cat keyfile1.pub >> /home/svnuser/.ssh/authorized_keys
cat keyfile2.pub >> /home/svnuser/.ssh/authorized_keys
cat keyfile3.pub >> /home/svnuser/.ssh/authorized_keys

Now I need to remove keyfile2.pub from authorized_keys file. So file 1 is keyfile2.pub and file 2 is authorized_keys.

Thank you!

TB0ne 09-16-2011 10:40 AM

Quote:

Originally Posted by chesschi (Post 4473312)
My distro is CentOS 5.5. I get empty results on that.

Here is the -f option:
Code:

-f FILE, --file=FILE
      Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.


Shouldn't matter what distro, and based on the examples you posted and the tests I ran, it works fine. It is important, however, to make sure you have the "-v" FIRST, before the -f to specify the file names.

chesschi 09-16-2011 10:49 AM

Quote:

Originally Posted by TB0ne (Post 4473729)
Shouldn't matter what distro, and based on the examples you posted and the tests I ran, it works fine. It is important, however, to make sure you have the "-v" FIRST, before the -f to specify the file names.

Weird.... I try on two different linux PC and got different results. One works one doesn't. By the way. Thank you!

TB0ne 09-16-2011 11:28 AM

Quote:

Originally Posted by chesschi (Post 4473737)
Weird.... I try on two different linux PC and got different results. One works one doesn't. By the way. Thank you!

Huh...maybe it's two different versions of grep. I'm using openSUSE 11.4, and it works fine. GNU grep 2.7...perhaps the options/flags are different on one of the versions you've got.


All times are GMT -5. The time now is 12:30 AM.