LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-06-2009, 10:09 PM   #1
kj6loh
Member
 
Registered: Jun 2004
Posts: 47

Rep: Reputation: 17
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
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

Rep: Reputation: 244Reputation: 244Reputation: 244
@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: 47

Original Poster
Rep: Reputation: 17
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



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

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

All times are GMT -5. The time now is 02:20 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