Sean
Sean

Reputation: 631

Determining What Line Does in Awk

I'm a very new beginner to awk. I'm reading over a simple loop statement where by using the split() command I have defined the 'a' array before the beginning of the loop and the 'b' array in each iteration of the loop.

Can someone help me with the statement below? I put it in to perspective since I know what the splits and for loop are doing.

split($2,a,":");
for(i=1,i<length(a),i++){
split(a[i],b," ")

#I don't know what the statement below this line does.
#It appears to be creating a multidimensional thing?


x[b[1]]=b[2]

Upvotes: 0

Views: 33

Answers (2)

zedfoxus
zedfoxus

Reputation: 37129

It looks like a single dimension array. Let's say if you had a text file with one line like this:

1|age 10:fname john:lname smith|12345
  • Assuming a delimiter of pipe symbol |, your $2 is going to be age 10:fname john:lname smith.
  • Split that by colon : will give 3 items: age 10, fname john and lname smith
  • for loops through these 3 items. It takes the first item age 10
  • It is split that up by space. b[1] is now age, b[2] is now 10
  • Array x['age'] is set to 10
  • Similarly, x['lname'] is set to smith and x['fname'] is set to 'john'

Upvotes: 1

Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

x[b[1]]=b[2]

It's not creating a multidementional array.

x is a array. it's assigning the value of array key b[z] to b[z]. z is a positive integer I just used here.

Upvotes: 0

Related Questions