The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing text in file1 with list from file2 dovetail Shell Programming and Scripting 9 07-24-2008 02:20 AM
parsing file2 with input from file1 forumsgr Shell Programming and Scripting 1 07-16-2008 07:09 AM
insert file2 after line containing patternX in file1 repudi8or Shell Programming and Scripting 5 04-18-2008 02:35 PM
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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-26-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1.

file1.txt (has empty first field)
Code:
:ABBATOM:B:H:1992
:ABBA TROJAN:B:H:1993
:ABBES FIRST HOPE:B:M:1997
:ABBEYS LENDING:BR:M:1996
:ABBEYS LEGACY:BR:M:1997
:ABBIE VELMA:B:M:1992
:ABBIESRUN:B:M:1995
file2.txt
Code:
31:ABBATOM:B:H:1992
32:ABBA TROJAN:B:H:1993
35:ABBIE VELMA:B:M:1992
36:ABBIESRUN:B:M:1995
DESIRED OUTPUT
Code:
31:ABBATOM:B:H:1992
32:ABBA TROJAN:B:H:1993
:ABBES FIRST HOPE:B:M:97
:ABBEYS LENDING:BR:M:1996
:ABBEYS LEGACY:BR:M:1997
35:ABBIE VELMA:B:M:1992
36:ABBIESRUN:B:M:1995
Any help would be appreciated.

Last edited by RacerX; 10-26-2008 at 08:36 PM.. Reason: made a boo boo
  #2 (permalink)  
Old 10-26-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,431
You should try to solve your problem before asking for help:
Code:
awk -F':' 'NR==FNR{_[$2]=$1;next}$2 in _{$1=_[$2]}1' OFS=':' f2 f1
  #3 (permalink)  
Old 10-27-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Thanks, i did try to solve it and did many searches here using keywords awk and compare. However, i kept screwing it up, am very confused at this point, and got nowhere and i really don't want to mess-up my 743,000 record database file, with something like:

Code:
FNR==NR { arr[$2]=$1; next}
$2 in arr { print arr[$1], $0 }

if (field1 of file2) == (field2 of file1) then
awk -F: 'FNR == NR {
 { f1[$2]=$1 $2
  next;
  }
if ($2 in f1) print  f1[$1]$0; else print $0;}' ~/Desktop/file2.txt ~/Desktop/file1.txt > ~/Desktop/outputfile.txt
  #4 (permalink)  
Old 10-27-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,431
Quote:
Originally Posted by RacerX View Post
.. i really don't want to mess-up my 743,000 record database file, with something like:
Always backup your working file.

Quote:
Originally Posted by RacerX View Post
Code:
FNR==NR { arr[$2]=$1; next}
$2 in arr { print arr[$1], $0 }
This will print only if $2 is in arr.


Quote:
Originally Posted by RacerX View Post
Code:
if (field1 of file2) == (field2 of file1) then   #why you need this test?
awk -F: 'FNR == NR {
 { f1[$2]=$1 $2                                  # why you need the value of $2
  next;
  }
if ($2 in f1) print  f1[$1]$0; else print $0
}
' ~/Desktop/file2.txt ~/Desktop/file1.txt > ~/Desktop/outputfile.txt
  #5 (permalink)  
Old 10-27-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Quote:
Always backup your working file.
Yes, words to live by, and backups have saved my rear-end many a time.

Quote:
if (field1 of file2) == (field2 of file1) then #why you need this test?
Sorry was thinking out loud and that line was actually commented out in program.

Quote:
{ f1[$2]=$1 $2 # why you need the value of $2
Told you i was totally confused.

Your solution worked and is much more elegant than anything i was working on:
Code:
awk -F':' 'NR==FNR{_[$2]=$1;next}$2 in _{$1=_[$2]}1' OFS=':' f2 f1
Thanks.
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 12:44 PM.


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