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 Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
how to delete line with matching text and line immediately after orahi001 UNIX for Dummies Questions & Answers 6 01-15-2008 12:34 AM
perl pattern matching vs. grep junkmail426 Shell Programming and Scripting 0 09-28-2005 10:40 AM
Grep all files matching partial filename mharley Shell Programming and Scripting 3 06-08-2005 02:17 PM
matching multiple times in same line oldtrash Shell Programming and Scripting 2 04-28-2004 05:57 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 08-09-2007
hemangjani hemangjani is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 73
Smile Grep Line with Matching Fields

Below is the scenario. Help is appreciated.

File1: ( 500,000 lines ) : Three fields comma delimited : Not sorted

1234FAA,435612,88975
1224FAB,12345,212356


File2: ( 4,000,000 lines ) : Six fields comma delimited (Last 3 field should match the 3 fields of File1) : Not Sorted :

0123456abcd,12345,abcdef,1234FAA,435612,88975
0123456wxyz,11234,lmnopq,1224FAB,12345,212356

I need to grab all the six fields for file2 when there is a match of first 3 fields of file1 and last 3 fields of file2.

I wrote a small script but seems like it might take days to complete :-)

1 #!/bin/ksh
2
3 while read record
4 do
5 cat file2 | grep "$record" >> final.list
6 done < file1

Can someone help me with a faster solution?

Thanks in advance.
  #2 (permalink)  
Old 08-09-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Code:
egrep -f file1 file2
  #3 (permalink)  
Old 08-09-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -F',' -f hem.awk file1 file2

hem.awk:
Code:
FNR==NR { f1[$0]; next}
( $(NF-2) FS $(NF-1) FS $(NF) ) in f1
  #4 (permalink)  
Old 08-09-2007
hemangjani hemangjani is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 73
Smile

vgersh99,

Linux box does not have nawk.
Has awk and gawk.


  #5 (permalink)  
Old 08-09-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by hemangjani View Post
vgersh99,

Linux box does not have nawk.
Has awk and gawk.


'gawk' is your new friend.
  #6 (permalink)  
Old 08-09-2007
hemangjani hemangjani is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 73
vgersh99,

The following ran for about 45-50 minutes and completed, but the output file was empty.
Could you please give me more insight on what is happening below?

Thanks

gawk -F',' -f hem.awk file1 file2 > final.list

hem.awk:
FNR==NR { f1[$0]; next}
( $(NF-2) FS $(NF-1) FS $(NF) ) in file1

ShellLife,

The egrep command ran over an hour and killed it.
I kicked it off again to see how long it runs.

Thanks
  #7 (permalink)  
Old 08-09-2007
RSAM2007 RSAM2007 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 2
Try this:
cat file1 | while read line
do
grep $line file2 >> file3

done
Closed Thread

Bookmarks

Tags
linux

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 12:50 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