The UNIX and Linux Forums  

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


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find , grep james94538 UNIX for Dummies Questions & Answers 3 10-09-2008 06:03 PM
grep and find MEllis5 UNIX for Dummies Questions & Answers 1 04-07-2008 05:16 AM
find then grep flame_eagle Shell Programming and Scripting 7 03-13-2008 08:19 AM
find and grep sarwan High Level Programming 4 04-10-2006 04:05 AM
find & grep Anika UNIX for Dummies Questions & Answers 11 02-01-2001 09:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-09-2008
Registered User
 

Join Date: Sep 2005
Posts: 9
grep, find or awk?

Since I found this very helpful forum and friendly people, I have a small request.

I use AIX Unix and would like to know if there's a simple way how to do a kind of compare between two files. Should I use some grep, find or awk?

I have 2 text files, let's name them file1 and file2.

What I need:

I want to read line by line file1 - certain field or better 2 fields of the file1 and look up the information in the file2.

To be more descriptive: I'd like to extract field1 at position 10-15 and field2 at position 25-31 from file1 and search or find if the same information is somewhere inside the file2. When no, I'd like to output the field1 & field2 off the file1 doesn't exit in file2.

let's say file1 contains something like:

123232 3232 2323 2323123123213 trterert

and file2 e.g.

123232 3232 2323 2323123123213 XXXXXXXX

and let's say I want 123232 & trterert from file1 to be searched in file2 ==> I get an "error" because the script would find only 123232 and not trterert.....

I'd be more than happy if someone can help me out or recommend a link or appropriate command or script.

I just need to check some record(s) in one file - and if they're available in the other file...

Please! Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 04-09-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
recent thread example
Reply With Quote
  #3  
Old 04-09-2008
Registered User
 

Join Date: Sep 2005
Posts: 9
Many thanks, going to look at it.
Reply With Quote
  #4  
Old 04-09-2008
Registered User
 

Join Date: Sep 2005
Posts: 9
tried the examples in the thread - not working

any suggestions?
Reply With Quote
  #5  
Old 04-09-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
So do I understand this correctly: if there is a line in file2 which does not contain both the information in columns 10-15 in file1, and the information in columns 20-25 in file1, then you want to print that line from file2, and additionally, any lines from file1 which did not have any such matches in file2?

Do the matches have to be from the same line in file1, or would, say columns 10-15 on line 1 and columns 20-25 on line 2 together constitute a match? (I'm guessing no, here.)

Can the matches be overlapping? Say you search for "12345" and "23456", then can the single value "123456" be considered a match on both of these fields at the same time? (I'm guessing this is okay, or doesn't matter in practice.)

Your examples don't match. Columns 10-15 in your file1 don't contain "123232" and columns 25-31 don't contain "treret". Can you come up with a correct example?

Code:
while read line; do
  grep "`echo "$line" | cut -c10-15`" file2 | grep "`echo "$line" | cut -c25-31`" || echo "$line" >&2
done <file1 | fgrep -vxf - file2
This will print non-matches from file1 to standard error and non-matches from file2 to standard output.

Using echo in backticks is always somewhat fragile if the values have significant leading or trailing whitespace. If the values you seek will not contain whitespace, this might even work, but it's hard to test without real-world samples.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0