LinuxQuestions.org
Visit Jeremy's Blog.
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-21-2011, 10:28 PM   #1
genROCK
LQ Newbie
 
Registered: Sep 2011
Posts: 10

Rep: Reputation: Disabled
Smile compare two files not line by line


hi everyone, I want to ask something for the following file:

file1:
909
919
929
932

file2:
909 9
929 9

output:
919
939

How can I do it in shell scripting. It cant be in diff, because diff is in line by line method. please assist, thanks for advance..
 
Old 09-21-2011, 11:24 PM   #2
packetsmacker
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 15
in your example you are comparing the the 1st value in the first txt file to the 1st value in the 2nd txt file. If that is correct you could read each txt file into an array then compare the arrays

here is a post about reading the txt files into arrays.
http://www.linuxquestions.org/questi...newbie-545840/

then you could just put it in a loop and check if the arrays are equal. Can't seem to find a good example of this right now. When I get break at work tomorrow I might be able to post a example.
 
Old 09-22-2011, 07:23 AM   #3
fatmac
Senior Member
 
Registered: Sep 2011
Location: Surrey/Hants Border, UK
Distribution: BSD & Linux - As long as they are Lightweight
Posts: 1,500

Rep: Reputation: Disabled
Perhaps you need to use 'uniq'

man uniq
 
Old 09-22-2011, 08:10 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
A simple awk program:
Code:
awk 'BEGIN {while ( getline < "file1" > 0 ) _[$1]++}{if ( $1 in _ ) print $1}' file2
 
Old 09-22-2011, 06:41 PM   #5
genROCK
LQ Newbie
 
Registered: Sep 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks guys for your assistance.. i gonna try it..
one more question? what if multiple files are being compared, make it generic..
not only same file are being compared..
thanks..

and the output will generate a new file.. thanks..

file1:
909
919
929
932

file2:
909 9
929 9

output(new file):
919
932

Last edited by genROCK; 09-22-2011 at 07:04 PM.
 
Old 09-22-2011, 08:29 PM   #6
genROCK
LQ Newbie
 
Registered: Sep 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
colucix, i try your code, but the output sees in the file2..
But thanks.. its great to know more about scripting..
 
Old 09-23-2011, 01:45 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by genROCK View Post
colucix, i try your code, but the output sees in the file2..
file2 is the input to the awk command. What do you mean exactly?
 
Old 09-25-2011, 09:59 AM   #8
genROCK
LQ Newbie
 
Registered: Sep 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
sorry for a confusing answer, i try your code but this is the output:

file1:
909
919
929
932

file2:
909 9
929 9

output:
909
929

I need to delete the same number in file1 and file 2, and in the new file that generates.. it will create the difference of file1 to file2. Thanks. this is only i need to finish my project.

This is needed output of new file:
919
932

Thanks for your help.
 
Old 09-25-2011, 10:13 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Sorry, my fault. Try to invert file1 with file2:
Code:
awk 'BEGIN {while ( getline < "file2" > 0 ) _[$1]++}!( $1 in _ )' file1
 
Old 09-27-2011, 07:38 PM   #10
genROCK
LQ Newbie
 
Registered: Sep 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks..
 
Old 04-04-2013, 01:25 PM   #11
vivaystn
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
thanks colucix!
 
  


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
How to compare two files line by line and print the line which is same. nancypriyanjali Programming 9 05-30-2011 11:04 PM
[SOLVED] kdiff3 doesn't allow me to compare line by line, any alternatives? TheIndependentAquarius Linux - Software 7 03-03-2011 06:33 PM
Compare two files line by line si-thk Linux - Newbie 2 03-24-2010 06:07 PM
compare two files in C and print the line when they do not match calorie712 Programming 7 04-13-2006 02:56 AM
How do I compare files line-by-line? spiffytech Linux - Software 2 01-05-2006 03:28 PM


All times are GMT -5. The time now is 10:50 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