Awk - Two equations?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk - Two equations?
# 1  
Awk - Two equations?

I really know barely anything about awk and the like but I have a bit of code I need help with.

The bash script is meant to get my usage numbers from my ISP and it does so perfectly. However I want to know how much I can use each day. So to do this I would need to divide its output by the number of days left on my quota. Here is the part I am stuck at:

Code:
echo $peak $days_remaining |awk '{ printf "Per day:         %.2f GB\n", (50-($1/1000000000)) }'

Which would output something like '19.36 GB' with the rest of the code. I know that adding '/$2' to the output would give the desired number but I'm not sure how to go about it.

Any ideas?
# 2  
Code:
set `expr 50 - $peak / 1000000000` | awk '{ printf "Use %.2f GB per day of total %.2f GB free.\n", ($1 / $days_remaining), $1}'

This User Gave Thanks to For This Post:
R0H0N
# 3  
Thanks for the quick response. I put your formatted line in but at first it produced nothing in terminal. I then added echo to the start and it produced:

Code:
awk: illegal field $(), name "days_remaining"
 input record number 1, file 
 source line number 1

# 4  
Code:
set `expr 50 - $peak / 1000000000` | echo $1 $days_remaining | awk '{ printf "Use %.2f GB per day of total %.2f GB free.\n", ($1 / $2), $1}'

This User Gave Thanks to For This Post:
R0H0N
# 5  
Quote:
Originally Posted by Light_
Code:
echo $peak $days_remaining |awk '{ printf "Per day:         %.2f GB\n", (50-($1/1000000000)) }'

Which would output something like '19.36 GB' with the rest of the code. I know that adding '/$2' to the output would give the desired number but I'm not sure how to go about it.

Any ideas?
Isn't this what you want...

Code:
echo $peak $days_remaining |awk '{ printf "Per day:         %.2f GB\n", (50-($1/1000000000))/$2 }'

Just added /$2 to your code
This User Gave Thanks to malcomex999 For This Post:
# 6  
Quote:
Originally Posted by R0H0N
Code:
set `expr 50 - $peak / 1000000000` | echo $1 $days_remaining | awk '{ printf "Use %.2f GB per day of total %.2f GB free.\n", ($1 / $2), $1}'

Code:
awk: division by zero
 input record number 1, file 
 source line number 1

---------- Post updated at 03:36 AM ---------- Previous update was at 03:32 AM ----------

Quote:
Originally Posted by malcomex999
Isn't this what you want...

Code:
echo $peak $days_remaining |awk '{ printf "Per day:         %.2f GB\n", (50-($1/1000000000))/$2 }'

Just added /$2 to your code
That works. Odd though, as I tried that earlier and it didn't produce the same result. Thank you and R0H0N also for helping troubleshoot.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #161
Difficulty: Easy
Application programmers are typically concerned only with interfaces in the application layer and often also in the transport layer, while the layers below are services provided by the TCP/IP stack in the operating system.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Try solver System of linear algebraic equations in Shell Bash

I want to try solving system of linear algebraic equations in Shell bash but i have any problems Value input is matrix and I dont know how to input matrix in Shell because that is dont support 2-dimensional array Please help me. Thank you so much (3 Replies)
Discussion started by: newbieseos
3 Replies

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

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. High Performance Computing

Differential Equations

I`m having a cluster with Rocks 5.2 distribution and I want to solve differential equations and I`m interested to know if are some programs already developed to do this. (3 Replies)
Discussion started by: rapo
3 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. UNIX for Dummies Questions & Answers

Can you perform mathematical equations in UNIX?

Hello one and all, I have a basic background in UNIX, but I was wondering if there is a way to perform simple mathematical equations (like multiplication, addition)? If so, is there a way to multiply values from a column by a value to produce a column with the answers? :confused: I am... (4 Replies)
Discussion started by: VioletFairy
4 Replies

Featured Tech Videos