Bash for loop with awk and variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash for loop with awk and variables
# 1  
Old 08-15-2014
Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables.

Code:
for i in $(seq 30 5 60)
do

# Define variables up and down in AWK
eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null)
eval $(awk 'BEGIN{ print "down=$((i-1))"}' < /dev/null)
echo $up
echo $down

# Print the longitude ($2) if the lattitude ($1) is between i =+/-1 and the age ($3) is less than 5
awk '{if ($1<$up && $1>$down && $3<5) print $2}' SWIR_age.xya > junk

# Display the profile number and average longitude
echo Profile $i
cat junk | awk '{sum+=$1} END {print sum/NR}'

done

# Remove junk
find . -name junk\* -exec rm {} \;

The echo command reads out the variables correctly but the next awk command can't read them. I've tried having the variables with and without the $ to no avail.

I'd also like to print all of this to a file at the end with the first column being profile$i and the second being the final value of sum/NR but again I am not sure how to do this.

Thanks,
Lily
# 2  
Old 08-15-2014
$up and $down in the awk script won't be expanded due to the single quotes. Be careful - in the script, $1 et al. are awk fields, not the shell script's positional parameters. And why do you use that dangerous (eval!) and cumbersome method to define the two variables? $i will not be available within awk - see above (will not be expanded, and if, would be an internal field). Nor will the shell arithmetic expression work.
# 3  
Old 08-15-2014
awk is not part of bash. awk is its own separate, independent language. Variables defined in BASH won't be defined in AWK unless you bring them in with -v VAR= at the beginning or VAR=... at the end.

If you run awk 5 times, you will be running it 5 independent times, with variables not carried between.

Variables inside single quotes ' ' do not expand -- and since awk code uses $ for column instead of variable, you don't want them to expand! The shell would eat them all and change the meaning of the awk code.

There's a couple ways to put variables into awk, none of which is embedding them inside the program itself.

Code:
awk -v VAR="$SHELLVARIABLE" '{ print VAR }' ...

awk '{ print VAR }' VAR="$SHELLVARIABLE" ...

awk does not need cat's help to read a file. You do this without the cat earlier, I'm not sure why you add it later.

Your awk code can be simplified so you only run it once instead of twice, and to get rid of the 'junk' file completely.

Code:
awk '($1<UP && $1>DOWN && $3<5) { TOTAL+=$2; TN++ } ; END { if(TN) print TOTAL/TN }' UP=$((i+1)) DOWN=$((i-1)) SWIR_age.xya

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Multiple variables using awk and for loop for web form submission

Hi My goal is to fill an HTML form and submit. What I have managed to do: 1. curl command to fill up the form and submit 2. a file which has the input curl command: curl -v -b cookie.txt -d __CSRFToken__=dc23d5da47953b3b390ec68d972af10380908b14 -d do=create -d a=open -d... (10 Replies)
Discussion started by: zorrox
10 Replies

4. Shell Programming and Scripting

Sending awk variables into curl in a bash script

Hello experts! I have a file1 with the following format (yr,day, month, hour,minute): 201201132435 201202141210 201304132030 201410100110 ... What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page. What I have... (6 Replies)
Discussion started by: phaethon
6 Replies

5. Shell Programming and Scripting

Find between lines start and end: awk with bash variables

I have a file as follows: 0 1056 85540 414329 774485 1208487 1657519 2102753 2561259 3037737 3458144 3993019 4417959 4809964 5261890 5798778 6254146 I want to find all lines between a specified start and end tag. (6 Replies)
Discussion started by: jamie_123
6 Replies

6. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

7. Shell Programming and Scripting

Bash for loop with two variables

Hi, I have the following folder structure: TST500-1000 TST500-2000 TST500-3000 TST700-1000 TST700-2000 TST700-3000 TST900-1000 TST900-2000 TST900-3000 I would like to store the numbers in the first column (considering "-" as column separator) into a variable then the numbers in... (3 Replies)
Discussion started by: alex2005
3 Replies

8. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question