using awk compare two variables in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk compare two variables in bash
# 1  
Old 12-22-2010
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.

Code:
awk 'BEGIN {printf "%.2f\n", '${bashArray[$count]}'>='$Variable' {print '${bashArray[$count]}'}}'

the command is it compare the values stored in an array to a variable and if the value in the array is greater than the variable to print the value of that array element.

if anyone could suggest why it doesn't work that'd be brill
# 2  
Old 12-22-2010
You can't give the result of the print command to printf, try this:
Code:
awk 'BEGIN {printf("%.2f\n", '${bashArray[$count]}'>='$Variable'?'${bashArray[$count]}':"")}'

This should always print a line, even if the condition is false.
To avoid that you can do something like:
Code:
awk 'BEGIN {if('${bashArray[$count]}'>='$Variable')printf "%.2f\n",'${bashArray[$count]}'}'

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 12-22-2010
the second one sort of worked but it won't let me compare floating numbers. which is what i was trying to get it to do.

---------- Post updated at 09:15 PM ---------- Previous update was at 08:22 PM ----------

don't worry just tried the first one and it does what i want, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

5. Shell Programming and Scripting

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. 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... (2 Replies)
Discussion started by: lily-anne
2 Replies

6. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

7. Shell Programming and Scripting

Compare and calculate two variables

Hi, I have two variables with some values with 5 decimal digits and separated with space, the values of the variables are different, I want to make operations with this two variables. The way was I find to do it itīs with files and paste file1 file2 | awk '{print $1 - $2}' I want to make it... (8 Replies)
Discussion started by: faka
8 Replies

8. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies

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

10. Shell Programming and Scripting

Compare two variables

Hi guys, How do I compare two variables using diff? The way I'm thinking: #!/bin/sh a=cat asdf.x | wc -l b=cat asdf.y |cut -d',' -f5 |grep -v '^$' |wc -l diff $a $b How to rewrite the above sciprt using only one line in command prompt? (2 Replies)
Discussion started by: onthetopo
2 Replies
Login or Register to Ask a Question