Register
It is currently Thu Apr 24, 2014 1:22 pm

awk to array


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Sep 04, 2010 2:05 am   

Joined: Sat Sep 04, 2010 1:51 am
Posts: 2
hi every1.

I am starting with bash scripting. And I am trying to make a script that populates an array variable from a few words of a text file.
This is to do later some comparisons between the array variables,

But I an stucked in populating the array. This is what I have so far:

mytextfile has:
aaa bbb ccc
ddd eee fff
ggg hhh iii
or something like...

while read LINE
do
awk '{print $2}'
done < mytextfile

this way I can print the second column. How can I pass the $2 from awk to an array variable?
I already understood a bit about arrays in bash, but the problem is from awk to variable... What should I do?

Thanks in advance.

daniel


Top
 Profile  
 PostPosted: Sat Sep 04, 2010 9:40 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
#!/bin/bash

myArray=( $(awk '{print $2}' < "myfile.txt") )

elems=${#myArray[@]}
echo "elements: $elems"

for (( i=0; i<elems; i++ )); do
  echo "myArray[$i] = ${myArray[$i]}"
done


Top
 Profile  
 PostPosted: Sat Sep 04, 2010 11:47 am   

Joined: Sat Sep 04, 2010 1:51 am
Posts: 2
Thank you!!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP