Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have two files of output.
File1 contains:
MachineObjID Name TypeID
File2 contains:
MCID MachineObjID Primary Secondary
I'd like have an awk script that will take $1 in File1, search for this in File2, then insert $2 from File1 into File2. So, the result will look something like:
MCID MachineObjID Name Primary Secondary
I know how to manipulate a single file, but don't know how to take a variable from one file to use in another. Any ideas?
thanks,
Koshare

Koshare,
I wrote a Awk / Bash script to do what you are wanting to do. There may be a easier way or more streamlined... but this script works. I developed and ran it on a Gentoo
Linux box, so the Solaris platform should not be a problem.
I decided to brush up on my scripting and awk... plus I'm starting a collection of code that I write for people on various forums so I can reference it when others
need the same info.
Here it is, enjoy:
---
#!/bin/bash
# Infinite Recursion
# 7/15/04
# Purpose: Computing.Net Support for user koshare,
# thread 10872
# Reading each line of the first data file
LINES=`cat data.txt`
for i in $LINES
do# Parsing each line into individual variables for # comparison. Notice the data files must be
# delimited with a ":".MOID=`echo $i | awk -F":" '{print $1}';`
NAME=`echo $i | awk -F":" '{print $2}';`
TYPEID=`echo $i | awk -F":" '{print $3}';`
# Grab the line from the second data file that contains the
# value that is being searched for from the first data file
# which is $1 or in this case $MOIDLINES2=`cat data2.txt | grep $MOID`
# Parse the line received from the second data file and
# assign the values into variables for use in the final string # construction.MCID=`echo $LINES2 | awk -F":" '{print $1}';`
MOID=`echo $LINES2 | awk -F":" '{print $2}';`
PRIM=`echo $LINES2 | awk -F":" '{print $3}';`
SEC=`echo $LINES2 | awk -F":" '{print $4}';`
# Sending the new line that contains what the values we
# want into a temporary fileecho $MCID " " $MOID " " $NAME " "
$PRIM " " $SEC >> temp.dat
done
--
The data and result files:
Grendel awk # cat data.txt
Grendel:Apache:Public
Beowulf:FTP:Private
Grendel awk # cat data2.txt
001:Grendel:user1:user2
002:Beowulf:user3:user4
Grendel awk # cat temp.dat
001 Grendel Apache user1 user2
002 Beowulf FTP user3 user4
-----
"cnet_koshare_10872.awk" 34L, 1118C
Hope it helps.
IR

awk 'BEGIN {i=0 ; while (getline < "File1") { l[i]=$0 ; i++ } } ; { for (j=0 ; j<=i ; j++) { split(l[j],f) ; if (f[1] == $2) print $1,$2,f[2],$3,$4} }' File2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |