awk array initialization at each line fails -
i have log file each line contains numbers separated ,
. wanted operation each number. seemed easy awk, somehow got stuck. array i'm using split each line, getting initialized once @ first line. after array not getting clear. split supposed clear array first use it, have used delete array
. still problem persists. appreciated.
below example; sample file
[[bash_prompt$]]$ cat log 1,2,3 2 9 1,4 5,7 7 8 6,2
this i'm getting
[[bash_prompt$]]$ awk '{print "new line " $1; delete a; split($1,a,","); for(var in a){ print "array element " var; } }' log new line 1,2,3 array element 1 array element 2 array element 3 new line 2 array element 1 new line 9 array element 1 new line 1,4 array element 1 array element 2 new line 5,7 array element 1 array element 2 new line 7 array element 1 new line 8 array element 1 new line 6,2 array element 1 array element 2
but below expecting
[[bash_prompt$]]$ awk '{print "new line " $1; delete a; split($1,a,","); for(var in a){ print "array element " var; } }' log new line 1,2,3 array element 1 array element 2 array element 3 new line 2 array element 2 new line 9 array element 9 new line 1,4 array element 1 array element 4 new line 5,7 array element 5 array element 7 new line 7 array element 7 new line 8 array element 8 new line 6,2 array element 6 array element 2
mine gnu awk 3.1.5. have found way using combination of shell , awk, ( running awk individually on each line) want know doing wrong
for (var in array)
sets var index of next element of array var
index, not contents. use
print "array element " a[var]
Comments
Post a Comment