saving values from awk expression into shell array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting saving values from awk expression into shell array
# 1  
Old 01-06-2009
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  
Old 01-06-2009
Quote:
Originally Posted by npatwardhan
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  
Old 01-06-2009
eval `awk '{print "arra["NR-1"]="$1}' a`
echo ${arra[@]}
# 4  
Old 01-07-2009
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  
Old 01-07-2009
Quote:
Originally Posted by npatwardhan
thanks. i used the following:

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


Sorry, my mistake. That should have been:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk saving field of first file into array

Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I... (15 Replies)
Discussion started by: RozenKristal
15 Replies

2. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

3. Shell Programming and Scripting

Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner. I use this code on ASCII files I need to report against. awk 'BEGIN { tokens = 0 tokens = 0 tokens = 0 } { for (token in tokens) { if ($1 == token){print $0; tokens++;}}} END {for (token in tokens){ if( tokens ==... (1 Reply)
Discussion started by: alan
1 Replies

4. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

5. Shell Programming and Scripting

Piping Unix Variable Array values into AWK

#ksh Here is my code: ERRORLIST="43032 12001 12002 12003 12004 34019 49015 49016 49017 49018 49024 49025 49026 58004 72003 12005 12006 12007 12008 12011 12012 16024 16023" for ERROR in ${ERRORLIST} do awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: '"${ERROR}"'/{print... (3 Replies)
Discussion started by: k1ko
3 Replies

6. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. Shell Programming and Scripting

fetching values using awk and storing into array

hi all I am using awk utility to parse the file and fetching two different vaues from two different record of a record set. I am able to see the result, now i want to store the result and perform some check of each values form database to mark valid and invalid. could you please help me... (3 Replies)
Discussion started by: singhald
3 Replies

8. Shell Programming and Scripting

saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows: 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, so far i have this: awk 'BEGIN {RS="\n";FS=","} { for(i=1;i<=NR;i++) { for(j=1;j<=NF;j++) { a=$j; } } (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. UNIX for Advanced & Expert Users

how do I store the values in array using shell

Hi, Is is possible to get the value using shell script? x=1 y1 = 10 y2 = 15 y3 = 7 echo $y$x is giving y1 (variable name) but I need the value of y1 (i.e. 10 dynamically) Is there any solution? if so, please mail me at kkodava@maxis.com.my ... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question