LinuxQuestions.org
Review your favorite Linux distribution.
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 09-06-2009, 10:09 PM   #1
kj6loh
Member
 
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

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
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#!/bin/sh

i=0

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


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

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

Rep: Reputation: 241Reputation: 241Reputation: 241
@OP, depending on what you wish to do, you can do everything in awk, no need to return values to bash.
Code:
awk '{a[++d]=$3}END{for(i=1;i<=d;i++){print a[i]} }' file
 
Old 09-07-2009, 12:36 PM   #5
kj6loh
Member
 
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().
 
  


Reply


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 02:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration