The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
saving awk value in a bash array variable npatwardhan Shell Programming and Scripting 1 12-17-2008 09:46 PM
Compare array values shantanuo UNIX for Dummies Questions & Answers 2 10-21-2008 05:41 AM
incorrect array values jhillier UNIX for Dummies Questions & Answers 6 01-04-2008 04:44 AM
to assign cut values to an array Syms UNIX for Dummies Questions & Answers 6 10-29-2007 06:42 AM
how do I store the values in array using shell krishna UNIX for Advanced & Expert Users 2 08-17-2001 09:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array.
i have:

Code:
exec 10<file2 
while read LINE <&10; do
   
    ARRAY1[$count]=$(awk '{print $1}' file2)
    ((count++))
done
echo ${ARRAY1[@]}
it prints just blank lines. file1 has two columns and i am trying to save the first column into an array..

this is not working.. any ideas what is wrong here?

thanks
  #2 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by npatwardhan View Post
hi i am trying to save the values i extract from a file with the help of awk in a bash shell array.
i have:

Code:
exec 10<file2 
while read LINE <&10; do
   
    ARRAY1[$count]=$(awk '{print $1}' file2)
    ((count++))
done
echo ${ARRAY1[@]}
it prints just blank lines.

What is in file2?
Quote:
file1 has two columns and i am trying to save the first column into an array..

If it is file1 that has two columns, why are you reading from file2?
Quote:
this is not working.. any ideas what is wrong here?

There are so many things wrong with that piece of code that it's hard to know where to begin.

That code is reading the entire file into an array for every line of the file. I tried it with a 9-line file, and it created 81 lines of output. If you got blank lines, you are probably reading the wrong file.

If you want to put the first field of a file into an array, there are many ways to do it. For example, using a while loop as you have:

Code:
count=0
FILE=file1
exec 3<"$FILE" ## Some shells only support FDs <= 9
while read a b <&3; do ## split the line as you read it
    ARRAY1[$count]=$a
    count=$(( $count + 1)) ## Use standard syntax for arithmetic
done
printf "%s\n" "${ARRAY1[@]}"
More efficient is:

Code:
## Assuming that the fields are separated by spaces
## Change the delimiter (and possibly IFS) if using something else
 
ARRAY1=( $(cut -d ' ' -f1 file1) )

Last edited by cfajohnson; 01-07-2009 at 08:59 PM..
  #3 (permalink)  
Old 01-06-2009
frostmourn frostmourn is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 8
eval `awk '{print "arra["NR-1"]="$1}' a`
echo ${arra[@]}
  #4 (permalink)  
Old 01-07-2009
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
thanks. i used the following:

Code:
ARRAY1=( cut -d ',' -f1 file1 )
file1 has the following:
Code:
1,5
2,9
3,8
does the first column from file1 get saved as array elements? because when i did echo ${ARRAY1[1]} i got a blank line. what am i doing wrong?
  #5 (permalink)  
Old 01-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by npatwardhan View Post
thanks. i used the following:

Code:
ARRAY1=( cut -d ',' -f1 file1 )

Sorry, my mistake. That should have been:

Code:
ARRAY1=( $(cut -d ',' -f1 file1) )
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0