Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK - replacing data with reference file

Status
Not open for further replies.

anamorph

IS-IT--Management
Mar 4, 2008
4
FR
Hello,

I have three files that i need to merde into one using awk:

file #1
GROUP;GADMIN

file #2
USER;GROUP

file #3
USER;UADMIN

i need to merge those files into the following sequence:
USER;GROUP;GADMIN;UADMIN

could you please help me out ?

Thanks in advance,

anamorph
 
What have you tried so far and where in your code are you stuck ?
Tip: you may consider associative arrays.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
well, i've tried ... that's a start ;)

I broke down the merge in two steps:

merge file #1 and file #2 then merge the result with file #3.

I've used paste() in the paste but this is no job for it.

I forgot to mention earlier, file #1 contains a liste of groups, a "reference file". basically the first step would be to find and complete file #2 by adding GADMIN to the existing fields: USER;GROUP.

thanks in advance for your help.
 
One option would be to sort the files and then use the join command twice to join them based on the appropriate key fields.

But since this is the awk forum, as PHV suggested, just read file #1 and file #3 into associative arrays indexed by GROUP and USER respectively, then read in file #2 and output the line and the contents of those two arrays for the appropriate indices.

Search this forum for "associative array" and you'll find many examples.

Annihilannic.
 
I have never used associative arrays before. could you help me out on this ?

regards
 
An associative array is just an array indexed by strings. Here's an example:

Code:
awk '
        # read data into array
        { number[$1]=$2}
        # access an array member
        END { print "mbili is " number["mbili"] }
' << HERE
moja 1
mbili 2
tatu 3
nne 4
HERE

Annihilannic.
 
here's what i did ...

tmp_analysis_phase_one="/tmp/tmp_$$_analysis.pt1"
tmp_analysis_phase_two="/tmp/tmp_$$_analysis.pt2"
tmp_analysis_phase_three="/tmp/tmp_$$_analysis.pt3"

for i in $tmp_analysis_phase_two
do
for z in $(cat $i)
do
if [ ! $z == "null" ]
then
ip_addr=$(nslookup $z | grep Name | sed 's/Name: //g' | awk -F"." '{print $1}')
uid=$(nbtstat -an $ip_addr | tail -4 | grep -v MAC | awk '{print $1}')
ip_addr_uid="$ip_addr;$uid"
echo $ip_addr_uid >> $tmp_analysis_phase_three
else
echo -e "no_ip_addr" >> $tmp_analysis_phase_three
fi
done
done

works like a charm :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top