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 > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 07-17-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,309
Code:
awk '
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}
a[$7]{$0=$0 FS a[$7]}
{print}
' file2 file1
Explanation:

The code for the first file (file2):

Code:
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}
NR==FNR -> is true when we read the first file.
split($2,s,"/") -> we split the second field to get the keys 2, 3 etc.
i=s[2] -> i is now the key
a[i]=$3 -> create an array "a" with the key as index and assign the value of the 3th field to the array
next -> read the next line and skip the rest of the code

The code for the second file (file1):

Code:
a[$7]{$0=$0 FS a[$7]}
{print}
a[$7]{$0=$0 FS a[$7]} -> if the 7th field exists in the array append a fieldseperator and the value of the array after the line (this is the 3th field of the first file)
{print} -> print the line.

Hope this helps.

Regards