Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-06-2009, 10:09 PM   #1
Registered: Jun 2004
Posts: 43

Rep: Reputation: 16
Awk output to bash array?

I there any way to output the awk output to a bash array?

For example

a = awk -F: '{print $3}' myfile

10 20 30 40 50
20 30 40 50 60
90 80 70 60 50

a[0] = 30 40 70

So that
a[1] = 30
a[3]= 70

or something similar
Old 09-06-2009, 10:40 PM   #2
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120


for line in `awk '{print $3}' myfile`; do
  i=`expr $i + 1`

echo ${arr[*]}
Old 09-06-2009, 10:47 PM   #3
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657
a=($(awk '{print $3}' myfile))
Old 09-07-2009, 12:19 AM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 242Reputation: 242Reputation: 242
@OP, depending on what you wish to do, you can do everything in awk, no need to return values to bash.
awk '{a[++d]=$3}END{for(i=1;i<=d;i++){print a[i]} }' file
Old 09-07-2009, 12:36 PM   #5
Registered: Jun 2004
Posts: 43

Original Poster
Rep: Reputation: 16
Thanks guys!

Yes I do know that I can do everything in AWK. but I want to avoid using system() if I can. Unless there's a way to test for the existence of a directory within awk NOT using system().


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM

All times are GMT -5. The time now is 03:55 PM.

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