LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk array (https://www.linuxquestions.org/questions/programming-9/awk-array-910075/)

grob115 10-25-2011 11:00 AM

awk array
 
Code:

grep 'detail' filename.log | awk '{variable[$1]=$1" "$6; for (index in variable) {print variable[index]}}'
It appears instead of storing and printing one line at a time. This stores all the lines into one index, and prints out this one cell index.

How can I make it to store one line, and print one line at a time?

grail 10-25-2011 11:18 AM

At the point of calling the for loop the array will be growing so on the first pass it will write the first value and then when the next line is read it will write
the first again and the second. Is this what you wanted?

Does the value of $1 constantly change? If not it will overwrite the previous value each time a new line is read.

grob115 10-25-2011 11:41 AM

Quote:

At the point of calling the for loop the array will be growing so on the first pass it will write the first value and then when the next line is read it will write
the first again and the second. Is this what you wanted?
Nah. I want it to print out all the index locations of the array once.

Quote:

Does the value of $1 constantly change? If not it will overwrite the previous value each time a new line is read.
Yes that's the intended behavior. I basically am interested in keeping only the last updated value. And yes $1 does change.

Nominal Animal 10-25-2011 09:19 PM

Perhaps
Code:

grep -e 'detail' filename.log | awk '{ variable[$1] = $1 " " $6 } END { for (i in variable) print variable[i] }'


All times are GMT -5. The time now is 06:41 AM.