saving awk value in a bash array variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting saving awk value in a bash array variable
# 1  
Old 12-17-2008
saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash:

Code:
total[$count]=`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:

Code:
./lines1: command substitution: line 24: unexpected EOF while looking for matching `"'
./lines1: command substitution: line 25: syntax error: unexpected end of file

the awk statement works when i save it in a single variable but does not seem to work when i use an array.
what is wrong with this? any help appreciated.
# 2  
Old 12-17-2008
Code:
total[$count]=`awk '{sum+=$3} END {print sum}' ${count}.txt`;

You just have too many double quote.
Remove it and try again.
Cheers,Smilie
X

Last edited by eXedon; 12-17-2008 at 09:51 PM..
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 results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. 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

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

Creating bash array name from variable

Hi gurus, I need to create arrays from variables, via a loop. The issue I have is with the array name creation. How do I use a variable to define an array? I want to do something like declare -a $H where $H is my loop variable. I then need to add items to each array I've created,... (3 Replies)
Discussion started by: melias
3 Replies

7. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 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 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: exec 10<file2 while read LINE <&10; do ARRAY1=$(awk '{print $1}' file2) ((count++)) done echo ${ARRAY1} it prints just blank lines. file1 has two columns and i... (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

Saving output from awk into a perl variable

How would I pass awk output to a perl variable? For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct. $test = system("awk '/NUMBER/{print \$4}' $_"); (8 Replies)
Discussion started by: userix
8 Replies
Login or Register to Ask a Question