LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2011, 11:00 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
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?
 
Old 10-25-2011, 11:18 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 10-25-2011, 11:41 AM   #3
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
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.
 
Old 10-25-2011, 09:19 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use array in awk rpd2207 Linux - Newbie 3 10-17-2011 07:30 PM
[SOLVED] printing array in awk ghantauke Linux - Newbie 3 11-24-2010 09:16 AM
[SOLVED] How to get search results to array by awk webhope Programming 5 05-05-2010 11:59 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Best awk array tutorial bijit1709 Programming 1 01-25-2008 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration