LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-15-2011, 12:11 PM   #1
chesschi
Member
 
Registered: May 2011
Posts: 41

Rep: Reputation: 0
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.

Last edited by chesschi; 09-16-2011 at 03:10 AM.
 
Old 09-15-2011, 12:30 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,886

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by chesschi View Post
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"
 
1 members found this post helpful.
Old 09-15-2011, 01:12 PM   #3
Aberrix
LQ Newbie
 
Registered: Sep 2011
Distribution: RHEL, AIX
Posts: 5

Rep: Reputation: Disabled
woops, nm just read you dont wanna use diff
 
Old 09-15-2011, 02:11 PM   #4
mmrtnt
LQ Newbie
 
Registered: Nov 2006
Location: las vegas, nevada
Distribution: fedora core x
Posts: 25

Rep: Reputation: 8
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.
 
Old 09-15-2011, 07:54 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mageia, and whatever VMs I happen to be playing with
Posts: 12,268
Blog Entries: 16

Rep: Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125Reputation: 3125
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.
 
Old 09-16-2011, 02:46 AM   #6
chesschi
Member
 
Registered: May 2011
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 09-16-2011, 02:47 AM   #7
chesschi
Member
 
Registered: May 2011
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Aberrix View Post
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.
 
Old 09-16-2011, 02:49 AM   #8
chesschi
Member
 
Registered: May 2011
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by mmrtnt View Post
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
 
Old 09-16-2011, 02:56 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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

Last edited by jschiwal; 09-16-2011 at 02:58 AM.
 
Old 09-16-2011, 02:58 AM   #10
chesschi
Member
 
Registered: May 2011
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by frankbell View Post
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!
 
Old 09-16-2011, 10:40 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,886

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by chesschi View Post
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.
 
Old 09-16-2011, 10:49 AM   #12
chesschi
Member
 
Registered: May 2011
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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!

Last edited by chesschi; 09-16-2011 at 10:59 AM.
 
Old 09-16-2011, 11:28 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,886

Rep: Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258Reputation: 4258
Quote:
Originally Posted by chesschi View Post
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.

Last edited by TB0ne; 09-16-2011 at 04:05 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how can I list the difference between two files? bones996 Linux - Software 3 04-04-2011 10:35 PM
How to extract difference between two files? Marshalle Linux - General 8 01-05-2011 12:45 PM
Difference between the MOUNTING files arooooon Linux - Newbie 2 11-08-2009 11:12 AM
The difference between .a and .so library files Harry Seldon Linux - General 5 12-14-2007 09:11 PM
.rar files versus .iso files the difference? dolphans1 Mandriva 14 01-31-2005 09:33 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration