The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 09:03 PM
grep and find MEllis5 UNIX for Dummies Questions & Answers 1 04-07-2008 08:16 AM
find then grep flame_eagle Shell Programming and Scripting 7 03-13-2008 11:19 AM
find and grep sarwan High Level Programming 4 04-10-2006 07:05 AM
find & grep Anika UNIX for Dummies Questions & Answers 11 02-01-2001 12:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-09-2008
netrom netrom is offline
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.
  #2 (permalink)  
Old 04-09-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,508
recent thread example
  #3 (permalink)  
Old 04-09-2008
netrom netrom is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 9
Many thanks, going to look at it.
  #4 (permalink)  
Old 04-09-2008
netrom netrom is offline
Registered User
  
 

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

any suggestions?
  #5 (permalink)  
Old 04-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0