|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 >file3Cheers, Ranga:-) |
| The Following User Says Thank You to rangarasan For This Useful Post: | ||
bobylapointe (06-17-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thank you guys for your answers !
|
|
#4
|
||||
|
||||
|
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 | ||
|
![]() |
| Tags |
| compare, diff, files, text |
| Thread Tools | Search this Thread |
| 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 |
|
|