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!

using data from another file in my script

Status
Not open for further replies.

bibby

Technical User
Jan 23, 2003
5
US
Hey,

I want to use information from another file in my script so I used this code

BEGIN {
while (getline < &quot;dict.txt&quot; > 0) {
dict[$1, $2] = $3
}
close (&quot;dict.txt&quot;)
}

to make an array of this file. Then I've put this file on the command line.
But now my script first prints this dict.txt and that's not what I want. If you understand me you may be able to help.
 
Yeah.
BEGIN doesn't understand your $1 $2 references except in some of the newer network and open() type code.

SO with a sample file like:
hi you
are not
really here
are you

and this code:
BEGIN {
while ((getline arr[a++] < &quot;filename&quot;) > 0) {
#be_quiet while you work
}
for (xx in arr) {
i = split(arr[xx],new,&quot;\t&quot;)
for (m=1 ; m <= i; m++) {
printf &quot;$%d=%s\n&quot;,m, new[m]
}
}
}'
Output looks like:

$1=hi
$2=you
$1=are
$2=really
$1=not
$2=here
$1=are
$2=you
 
Hi bibby,

I am not sure what you mean by &quot;my script&quot;.
Is't an Unix script or an awk script ?

Here is an example of an awk program that read a file in an array and uses this array.
In the BEGIN pattern, it reads a file &quot;colors.txt&quot; that define colors by their RGB values.
In the action, it adds the name of the color defined by $1 $2 $3 at the end of the input file lines.

awk -f AddColorName.awk input_file

-- colors.txt --
0 0 0 Black
0 0 255 Blue
0 125 0 Green
255 0 0 Red
255 165 0 Orange
255 255 0 Yellow
255 255 255 White

-- Input file --
255 255 255 Banana
0 255 0 Lime
255 0 0 Apple
0 0 0 Night

-- Output --
255 255 0 Banana Yellow
0 255 0 Lime
255 0 0 Apple Red
0 0 0 Night Black

-- AddColorName.awk --
BEGIN {
while (getline < &quot;colors.txt&quot; > 0) {
colors[$1, $2, $3] = $4
}
close (&quot;colors.txt&quot;)
}
{
if ($1 SUBSEP $2 SUBSEP $3 in colors) {
$(NF+1) = colors[$1, $2, $3] ;
}
print $0 ;
} Jean Pierre.
 
OLD awk's don't support multi-dimensional arrays.

If on SUN, use either nawk or /usr/xpg4/bin/awk

Also make sure you refer to the multi-dimesional arrays in the correct way - &quot;man nawk&quot;

vlad
 
yeah, too bad aigles doesn't know that you need input for
the main awk portion, and that this:
while ((getline < &quot;file&quot;) > 0) {
bleah[$1] = $2
}
is going to be meaningless in the main block or at
least is not going to give you all records.
try it with input from the same file you just loaded
in the begin block and see what happens, then try it
without input as in aigles example.
Stick with my example above.
 
If your version of awk don't support multi-dimensional arrays, replace :

{
ARRAY2[i, j] = 2
ARRAY3[i, j, k] = 3
}

by

BEGIN {
SUBSEP = &quot;\034&quot;
}
{
ARRAY2[i SUBSEP j] = 2
ARRAY3[i SUBSEP j SUBSEP k] = 3
}

In fact, it's what awk is doing when you specify more than one index. All arrays have just one dimension. Jean Pierre.
 
Jeez, what a kludge.
Do it all in the begin block.

Otherwise you have to read the file twice, and
check to see that the reread $elements match the
previous(BEGIN)arrays elements AND rebuild the
array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top