Hello,
I am trying to use the awk getline command to populate an array from a flat file.
Has anyone done this before?
So we have file1:
a
b
c
d
I want to create an array like index[x] that contains these four elements
Hello,
I am trying to use the awk getline command to populate an array from a flat file.
Has anyone done this before?
So we have file1:
a
b
c
d
I want to create an array like index[x] that contains these four elements
Why use awk? I can't test it right now but wouldn't :
set -A index $(cat yourfile)
do the trick or do you want to load yourfile while inside an awk program ?
I wouldn't bother with getline...
$ cat > awk_script
#!/usr/bin/awk -f
{
a[i++]=$0
}
END {
for ( x = 0; x < NR; x++ ) {
print "Element is..." , a[x]
}
}
$ chmod +x ./awk_script
$ ./awk_script ./file1
Element is... a
Element is... b
Element is... c
Element is... d
Cheers
ZB
when executing this is get: i++: bad number
I need to load the file whilst inside the awk program
if on Solaris, try using nawk like so:
#!/usr/bin/nawk -f
i'm running on AIX, i've tried so many variations but can't get to the bottom of this , any help appriciated
moved to a new thread
Instead of
a[i++]=$0
try:
a[NR]=$0