Arrays in awk 
Author Message
 Arrays in awk

Hi there,

I'm trying to read the contents of a file into an awk array. So far I
cannot get it to work using the system variable, FILENAME.

This is my one line script:

#!/bin/sh
awk 'FILENAME == "fruits" { array[NR] = $1 } END { for ( x = 1; x <= NR;
x++ ) print array[NR] }'

The conents of the file "fruits" are:

apple
orange
pear
pineapple
peach
nectarine

Any ideas ?

Thanks,

Carlos



Tue, 26 Apr 2005 05:47:01 GMT  
 Arrays in awk

Quote:
> Hi there,

> I'm trying to read the contents of a file into an awk array. So far I
> cannot get it to work using the system variable, FILENAME.

> This is my one line script:

> #!/bin/sh
> awk 'FILENAME == "fruits" { array[NR] = $1 } END { for ( x = 1; x <=
NR;
> x++ ) print array[NR] }'

    ... print array[x]

perhaps?

Quote:

> The conents of the file "fruits" are:

> apple
> orange
> pear
> pineapple
> peach
> nectarine

> Any ideas ?

> Thanks,

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.


Tue, 26 Apr 2005 06:16:48 GMT  
 Arrays in awk


[...]
% This is my one line script:
%
% #!/bin/sh
% awk 'FILENAME == "fruits" { array[NR] = $1 } END { for ( x = 1; x <= NR;
% x++ ) print array[NR] }'

What this will do is load up array whenever the file listed on the
command-line is called fruits, then (as Peter noted) print the last
line repeatedly. The problem is that you don't list the input file on
the command line. You might try

 #!/bin/sh
 awk '{ array[NR] = $1 } END { for ( x = 1; x <= NR;
 x++ ) print array[x] }' fruits

or, if you want to avoid listing the file on the command-line at all,

 #!/bin/sh
 awk 'BEGIN { ARGC = 2; ARGV[1] = "fruits" }
      { array[NR] = $1 }
      END { for ( x = 1; x <= NR; x++ ) print array[x] }'

Note also that #! syntax does not restrict you to using a shell. You
could have

 #!/usr/bin/awk -f
 BEGIN { ARGC = 2; ARGV[1] = "fruits" }
 { array[NR] = $1 }
 END { for ( x = 1; x <= NR; x++ ) print array[x] }

[on most machines -- Solaris of any vintage and some other systems of
 older vintages require a different program path in the #! line]
--

Patrick TJ McPhee
East York  Canada



Tue, 26 Apr 2005 07:58:23 GMT  
 Arrays in awk
Hi Carlos,

Maybe try

 #!/bin/sh
 awk '{ i++;array[i] = $1; } END { for ( x = 1; x <= i; x++ ) print
array[x] }' fruits
it should work

Or, I did not tried ,but maybe it works also:

#!/bin/sh
awk 'BEGIN{FILENAME == "fruits";} { i++;array[i] = $1 } END { for ( x = 1; x
<= i; x++ ) print array[x] }'

Regards,
-Joseph


Quote:
> Hi there,

> I'm trying to read the contents of a file into an awk array. So far I
> cannot get it to work using the system variable, FILENAME.

> This is my one line script:

> #!/bin/sh
> awk 'FILENAME == "fruits" { array[NR] = $1 } END { for ( x = 1; x <= NR;
> x++ ) print array[NR] }'

> The conents of the file "fruits" are:

> apple
> orange
> pear
> pineapple
> peach
> nectarine

> Any ideas ?

> Thanks,

> Carlos



Tue, 26 Apr 2005 23:16:38 GMT  
 Arrays in awk

Quote:

>  awk '{ i++;array[i] = $1; } END { for ( x = 1; x <= i; x++ ) print
> array[x] }' fruits
> it should work

> Or, I did not tried ,but maybe it works also:
> awk 'BEGIN{FILENAME == "fruits";} { i++;array[i] = $1 } END { for ( x = 1; x
> <= i; x++ ) print array[x] }'

Hello,
        the second example wouldn't work.  I guess you have meant
FILENAME = "fruits"; but it wouldn't work either.

It is possible to do this:

awk 'BEGIN{ARGV[1]="slon";ARGC++}; { i++;array[i] = $1 };
   END { for ( x = 1; x <= i; x++ ) print array[x] }

But I see no advantage over the first solution.

(And note that I added two semicolons: it's a good habit to separate the rules
by semicolons, if they happen to be on one line.)

HTH,
        Stepan Kasal



Fri, 29 Apr 2005 17:46:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Arrays in awk/awk help please!

2. Learning Arrays in AWK

3. Unwind a shell array with awk

4. Can I use arrays in AWK?

5. AWK using an array to subscript an array??

6. perl hash vs awk array

7. awk array elements

8. awk array

9. AWK - Multi-Deminsional Arrays

10. Looking for Unix-based AWK that does Array sorting

11. array elements in (n)awk

12. Associative Array with PC AWK

 

 
Powered by phpBB® Forum Software