Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-06-2011   -   Original Discussion by right_coaster
right_coaster's Unix or Linux Image
right_coaster right_coaster is offline
Registered User
 
Join Date: May 2010
Last Activity: 26 June 2012, 4:31 PM EDT
Posts: 18
Thanks: 5
Thanked 1 Time in 1 Post
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it!

So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following:

If $1 and $2 in file1 equals $1 and $2 in file2, then return the the values of $1, $2 and $4 in file1 IF $5 in file1 is greater than $3 in file2

I could put a loop together but I think that would be very inefficient compared to "if" this AWK command could be extended.

file1


Code:
 
PRODCON1|SYSTEM.IOA|040720101327|USER1|201004071327
PRODCON2|SYSTEM.TEST|040720101327|USER1|201110010932
SA|SYSTEM.CCR|040720101327|USER1|201004071327
US|SYSTEM.PPD|040720101327|USER1|201004071327
VP|SYSTEM.IOA|040720101327|USER1|201110012346

file2


Code:
 
PRODCON1|SYSTEM.IOA|201109201128
PRODCON2|SYSTEM.TEST|201109201128
SA|SYSTEM|201109201128
US|SYSTEM|201109201128
VP|SYSTEM.IOA|201109201128

First I match $1 and $2 in both files with this:


Code:
awk -F\| 'NR == FNR {i[$1, $2]; next}(($1, $2) in i )' file2 file1

The result contains only the lines in file1 where $1 and $2 are the same in both files:


Code:
 
PRODCON1|SYSTEM.IOA|040720101327|USER1|201004071327
PRODCON2|SYSTEM.TEST|040720101327|USER1|201110010932
VP|SYSTEM.IOA|040720101327|USER1|201110012346

Now, from these results, I need to determine if $5 in the output above (which is date format YYYYMMDDHHmm) is greater than $3 in file2 and display $1, $2, and $4 from file1 where this conditions exists:

Required final output:


Code:
 
PRODCON2|SYSTEM.TEST|USER1
VP|SYSTEM.IOA|USER1

Sponsored Links
    #2  
Old Unix and Linux 10-06-2011   -   Original Discussion by right_coaster
birei's Unix or Linux Image
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 2 December 2017, 5:58 AM EST
Location: Madrid, Spain
Posts: 628
Thanks: 8
Thanked 174 Times in 171 Posts
Hi right_coaster,

Try:



Code:
$ cat file1
PRODCON1|SYSTEM.IOA|040720101327|USER1|201004071327
PRODCON2|SYSTEM.TEST|040720101327|USER1|201110010932
SA|SYSTEM.CCR|040720101327|USER1|201004071327
US|SYSTEM.PPD|040720101327|USER1|201004071327
VP|SYSTEM.IOA|040720101327|USER1|201110012346
$ cat file2
PRODCON1|SYSTEM.IOA|201109201128
PRODCON2|SYSTEM.TEST|201109201128
SA|SYSTEM|201109201128
US|SYSTEM|201109201128
VP|SYSTEM.IOA|201109201128
$ awk -F\| 'NR == FNR {i[$1, $2] = $3; next} { if ( i[$1, $2] && i[$1, $2] < $5 ) { printf( "%s|%s|%s\n", $1, $2, $4 ) } }' file2 file1
PRODCON2|SYSTEM.TEST|USER1
VP|SYSTEM.IOA|USER1

Regards,
Birei
The Following User Says Thank You to birei For This Useful Post:
right_coaster (10-06-2011)
Sponsored Links
    #3  
Old Unix and Linux 10-06-2011   -   Original Discussion by right_coaster
right_coaster's Unix or Linux Image
right_coaster right_coaster is offline
Registered User
 
Join Date: May 2010
Last Activity: 26 June 2012, 4:31 PM EDT
Posts: 18
Thanks: 5
Thanked 1 Time in 1 Post
Awesome, works nicely... and I'll run it on my 12,000 line files and see how it goes. Thanks again.
    #4  
Old Unix and Linux 10-06-2011   -   Original Discussion by right_coaster
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 February 2018, 2:35 PM EST
Location: Foxborough, MA
Posts: 8,216
Thanks: 235
Thanked 795 Times in 741 Posts
Quote:
Originally Posted by birei View Post
Hi right_coaster,

Try:



Code:
$ cat file1
PRODCON1|SYSTEM.IOA|040720101327|USER1|201004071327
PRODCON2|SYSTEM.TEST|040720101327|USER1|201110010932
SA|SYSTEM.CCR|040720101327|USER1|201004071327
US|SYSTEM.PPD|040720101327|USER1|201004071327
VP|SYSTEM.IOA|040720101327|USER1|201110012346
$ cat file2
PRODCON1|SYSTEM.IOA|201109201128
PRODCON2|SYSTEM.TEST|201109201128
SA|SYSTEM|201109201128
US|SYSTEM|201109201128
VP|SYSTEM.IOA|201109201128
$ awk -F\| 'NR == FNR {i[$1, $2] = $3; next} { if ( i[$1, $2] && i[$1, $2] < $5 ) { printf( "%s|%s|%s\n", $1, $2, $4 ) } }' file2 file1
PRODCON2|SYSTEM.TEST|USER1
VP|SYSTEM.IOA|USER1

Regards,
Birei
a slight mod:


Code:
awk -F\| 'NR == FNR {i[$1, $2] = $3; next} i[$1, $2] && i[$1, $2] < $5 { print $1, $2, $4 }' OFS='|' file2 file1

The Following User Says Thank You to vgersh99 For This Useful Post:
birei (10-06-2011)
Sponsored Links
    #5  
Old Unix and Linux 10-06-2011   -   Original Discussion by right_coaster
right_coaster's Unix or Linux Image
right_coaster right_coaster is offline
Registered User
 
Join Date: May 2010
Last Activity: 26 June 2012, 4:31 PM EDT
Posts: 18
Thanks: 5
Thanked 1 Time in 1 Post
Using either of these methods, is it possible to print a field from file2?

NOTE: I added printing $5 below to get the date from file1 and if at all possible I'd like to print the date ($3) from file2 at the end. I tried to reference back to it with the i variable but it keeps bombing.



Code:
awk -F\| 'NR == FNR {i[$1, $2] = $3; next} i[$1, $2] && i[$1, $2] < $5 { print $1, $2, $4, $5 }' OFS='|' file2 file1
PRODCON2|SYSTEM.TEST|USER1|201110010932
VP|SYSTEM.IOA|USER1|201110012346

Desired: (data is from file1 except last entry is $3 from file2)


Code:
PRODCON2|SYSTEM.TEST|USER1|201110010932|201109201128
VP|SYSTEM.IOA|USER1|201110012346|201109201128

---------- Post updated at 06:07 PM ---------- Previous update was at 05:34 PM ----------

Aha! / Duh! moment... got it, thanks again for the helpful posts.



Code:
 
awk -F\| 'NR == FNR {i[$1, $2] = $3; next} i[$1, $2] && i[$1, $2] < $5 { print $1, $2, $4, $5, i[$1, $2] }' OFS='|' file2 file1

The Following User Says Thank You to right_coaster For This Useful Post:
vgersh99 (10-06-2011)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read Field from file1 and find and replace in file2 gdevadas Shell Programming and Scripting 8 04-07-2011 06:28 PM
Awk Compare File1 File2 on f2 RacerX Shell Programming and Scripting 4 10-27-2008 10:50 AM
how do i pattern match a field with awk? someone123 Shell Programming and Scripting 4 06-03-2008 09:08 AM
match value from file1 in file2 myguess21 Shell Programming and Scripting 2 02-21-2008 11:39 AM
Awk Compare f1,f2,f3 of File1 with f1 of File2 RacerX Shell Programming and Scripting 6 11-09-2007 01:34 AM



All times are GMT -4. The time now is 04:30 PM.