Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-17-2012
Registered User
 
Join Date: Jun 2012
Posts: 16
Thanks: 9
Thanked 0 Times in 0 Posts
Compare two text files

Hello guys,

I have file1 and file2, two text files containing various lines.
I'm trying to find a way to compare file1 and file2:

If the first 7 characters of a line in file2 match the first 7 characters of a line in file1, then do not do anything.
Print out the lines of file1 (in file3, let's say) not found in file2 (according to this 7 characters rule).

I looked at "diff" but couldn't find a way to specify how similiar (first 7 characters matching) the lines have to be.


What utility should I use according to you?

Cheers !!
Sponsored Links
    #2  
Old 06-17-2012
rangarasan's Avatar
Registered User
 
Join Date: Jul 2011
Location: Chennai, India
Posts: 484
Thanks: 9
Thanked 119 Times in 115 Posts
Awk

Hi,

Try this one,

Code:
awk 'NR==FNR{k=substr($0,1,7);a[k]=1;next;}{k=substr($0,1,7);if(!a[k]){print;}}' file2 file1 >file3

Cheers,
Ranga:-)
The Following User Says Thank You to rangarasan For This Useful Post:
bobylapointe (06-17-2012)
Sponsored Links
    #3  
Old 06-17-2012
Registered User
 
Join Date: Jun 2012
Posts: 16
Thanks: 9
Thanked 0 Times in 0 Posts
Thank you guys for your answers !
    #4  
Old 06-17-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,350
Thanks: 144
Thanked 1,756 Times in 1,593 Posts
It could be shortened a little:

Code:
awk '{k=substr($0,1,7)} NR==FNR{a[k];next} !(k in a)' file2 file1>file3

Sponsored Links
Closed Thread

Tags
compare, diff, files, text

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To compare the content of two text files rammm Shell Programming and Scripting 11 04-25-2012 02:47 AM
How to compare two text files nivas_k2006 Shell Programming and Scripting 1 12-01-2011 08:19 AM
Compare 2 files and output only the different text. hakermania Shell Programming and Scripting 2 01-26-2011 03:34 PM
Compare 200,000 of rows in two text files ppat7046 Shell Programming and Scripting 1 04-03-2009 05:04 PM
compare text files jimmyflip UNIX for Dummies Questions & Answers 14 05-28-2002 07:02 AM



All times are GMT -4. The time now is 11:53 PM.