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!

Creating an awk array from more than 1 field in multi field line

Status
Not open for further replies.

icalderon

Technical User
Oct 24, 2008
2
US
hello all,

currently i am attempting to create an array in awk using a flat file as the source. the flat file format is as such:


word1 word2 word3

so i am attempting to use the NF awk parameter in order to assign values to the elements in the array. unfortunately this is not working just yet.

here is the code i currently have:

export x=0
delim=`awk '{print NF}' array_source.out`
export delim
echo $delim "is the value of the delimiter"

until (( x == delim ))

do
echo $x "is the pre math value for counter"

test[x]=`awk "BEGIN{ y = "$x" } NF == y { print $1 }" array_source.out`
x=`expr $x + 1`
echo $test[x]


echo $x "is the post math value for counter"
# echo $test[cnt1]
done

I am attempting to use NF to control which field is shoved into the array. By using a delimiter equal to NF in the source file that should control the loop as its delimiter.

However for some reason I cant get the array populated.

What am I missing here? I used this logic to create an array based on NR but this is new since it is a single line source file and I am using NF as a controller.

Any help would be GREAT! Tx!
 

Your script makes no sense.
No (clearly) stated requirement.
No sample data.
=
No help.
[thumbsdown]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
It looks like you're not actually creating the array in awk, but just using awk to pull out the individual elements in the file to set up a shell array variable.

How about just using set -A test $(cat array_source.out)?

Annihilannic.
 
Tx Annihilannic! The set -A did the job just fine.

Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top