This tag is meant for everything that has to do with mathematical operations. So if you want to ask a question about doing calculations in `awk`, or if you have a question about `bc`, this is the right tag to use.

learn more… | top users | synonyms

0
votes
1answer
31 views

Convert a column of epochs into a time column starting from 0

What I have Hi, imagine a lot of this files, where first column is epoch, and the other(s) column(s) are some data: 1000333,34,1 1001456,56,0 1005356,34,2 What I need I need to transform them ...
0
votes
1answer
32 views

awk: arithmetic does not work on a particular field

I have several lines such as: N2O + M = N2 + O + M 1.300E+11 0.00 59620.0 I need to substitute the third from last field with a factor of itself which does not work ...
3
votes
0answers
58 views

Shell Arithmetic Expansion with Quotes

In Bash and Dash, using quotes in an Arithmetic Expansion is illegal: $ bash -c 'x=123;echo $(("$x"))' bash: "123": syntax error: operand expected (error token is ""123"") $ dash -c 'x=123;echo ...
3
votes
3answers
42 views

How to treat numbers starting with zeroes as decimal integer

Input would be file name like following : A-B-000001-C A-B-000002-C ..... ..... A-B-999999-C All file should be sequential. I want to find missing sequential file names. For this I'm separating 6 ...
0
votes
1answer
32 views

arithmetic operation with columns in text file

I have a data file which consists of two columns. I need to perform arithmetic operation with these columns as follows; column_1=a(i) column_2=b(i) where i is the line number ...
1
vote
1answer
48 views

Find a “moderately large” divisor of a given number?

After processing some data, I get out a file that has a certain number of data points in it --- one per line. I need to pass these data points on to another tool that will do more number crunching --- ...
2
votes
2answers
62 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
1
vote
1answer
33 views

Modify a commands to an original values

I am working with terminal in Linux and have some problems. I need to make a script contains these commands in pico and then to execute it. I've already done this and my output is 254 9 23 -45 and ...
0
votes
2answers
77 views

How to increment local variable in Bash?

Data 1 \begin{document} 3 Code #!/bin/bash function getStart { local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)" echo $START } START2=$(getStart) echo ...
0
votes
1answer
39 views

I'm trying to write a script to compare results from a code to my already established results for regression testing

I've been trying to write a bash script to run my code, then compare the final output to a previous run (with the correct result) under regression.dat. The goal is for it to tell me if the code ...
1
vote
4answers
105 views

Show ratio for all available resolutions

I tried to get the ratio with this script: echo "$(xrandr)"|cut -f 4 -d" "|sed 's/x/00 \\\* /g'|while read i; do echo "$i = "$(expr $i); done but I get errors like 102400 * 768 = expr: ...
3
votes
2answers
340 views

Subtract floating numbers in shell script

I'm trying to do some calculation in shell script with CPU usage. Which return floating point number. But when I subtract this number I'm getting error. See the following code and error. Code ...
1
vote
2answers
193 views

Copy Single Value from a file to a variable and perform arithmetic over it? [duplicate]

Suppose we have a file like 'abc.txt' which has a single value 123456 and file 'xyz.txt' which also has a single value like 654321. I want to store these values form the file to some shell variable ...
1
vote
1answer
44 views

Add list of numbers like an accountant

How can I add a list of numbers like an accountant would (using presumably bc or dc)? Typically this is done by entering each number followed by a "+" key, e.g., there would be on an adding machine. I ...
1
vote
1answer
65 views

Loop over lines of file, partition file and classify each partition

In the following I've put up my code for iterating over lines of a file in order to collect data within a segment from the file in following fashion: Illustration for understanding procedure: segL ...
1
vote
1answer
78 views

increment a value pulled from a file

I have a cron that I am writing that will repeat every hour, and I need a simple way to pass a counter from the last run to the next run. My plan was to add the number to a file at the end, and then ...
2
votes
2answers
288 views

Assign variable identifier in loop and echo its value

I hope this question is not a duplicate because I've been trying various ways to solve this problem but couldn't find any solution. I have a for loop in which I'd like to store a command output into ...
23
votes
2answers
2k views

What does a dollar sign followed by a square bracket mean in bash?

It appears that $[expr] performs arithmetic expansion just like $((expr)). But I can't find any mention of $[ in the bash manual. This command gives no results: gunzip -c ...
0
votes
4answers
207 views

Hexadecimal number sequence checker in Linux?

I have a list of hexadecimal number. I would like to check whether they are in sequence or not. That is, they should be consecutive numbers, in increasing order. In other words, there should be an ...
1
vote
2answers
346 views

Aliasing using for or while loop in .bashrc

Can I use a for or while loop when I have similar pattern in the aliases that I put in my .bashrc file? For example, I have: alias c0='ssh computer-1-0' alias c1='ssh computer-1-1' alias c2='ssh ...
2
votes
1answer
87 views

bash + how to calculate very long numbers in bash? [duplicate]

how to calculate very long numbers in bash? param=$(( 3247238523785623478565 + 53453453252345346534563412634 )) echo $param 3420247196502465471 as we see here this isnt the right answer , because ...
1
vote
1answer
125 views

bash: -eq vs. == and `bc` output type

This snippet of code has been taken from a larger script, (humbly) simulating the non-existing abs() function: [[ $(echo "$val < 0" | bc) -eq 1 ]] && val=$(echo "$val * -1" | bc) So we ...
3
votes
1answer
48 views

Moving sub-directory to new parent directory where the new directory name is incremented by 1

I am attempting to move a sub-directory from one parent directory to another for hundreds of instances, while changing the name of the sub-directory during the move. My directories are a set of ...
0
votes
2answers
76 views

Script with multiple arithmetic conditions failing

I'm experiencing problems with a script converting numbers to bytes. If the number is large enough to hit gigabytes or terabytes, the script runs as it should. If the number converts to kilobytes, ...
1
vote
2answers
1k views

How to format decimal number to display the preciding zero but not the trailing ones?

I have the following script on Ubuntu 14.04.1 LTS 64-bit bash command prompt: S=0.030 F=0.150 N=10 reso=20 DIFF=`echo "scale=3; $F - $S" | bc -l` df=`echo "scale=3; $DIFF / $N" | bc -l` is=`echo ...
1
vote
1answer
39 views

Comparing variables in arithmetic

Was wondering if someone can take the time to explain the following: I have a directory of files (PDF), which I place into an array. shopt -s nullglob # Set array ...
1
vote
1answer
624 views

How can subtract 2 floats which have been extracted from 2 other files with BASH [duplicate]

I'm trying to get the difference between two numbers which I have taken from two files. I think my code will make sense: I've tried to make it work by two different methods, didn't work. What I get ...
4
votes
4answers
882 views

How to sum time using bash?

I want to know the total amount of time that a series of processes would take in my computer to decide if I should running there or in a stronger computer. So, i am forecasting the running time of ...
0
votes
2answers
149 views

Integer input constraints

I want to make an input constraint on read: Terminal Shell; read x read y echo $(($x+$y)) I want to make a constraint of x, I want x to be -100 <= x <= 100. What's the command I insert ...
0
votes
1answer
2k views

“too many arguments in [ (test) statement”

this is my first "if-else" script on linux. It gives the error [: too many arguments Code: n=0 if [ $n % 2 -eq 0 ] then echo "even number" fi How to correct it.
1
vote
1answer
47 views

Whats wrong in creating/printing this array?

To find the average time taken to create certain files, I'm using this minutes array, then would simply use bash arithmetic to find the average. However I'm unable to get the difference except for the ...
2
votes
1answer
172 views

Problematic bc calculation in shell script

On Ubuntu 14.04.1 64-bit LTS I am writing a shell script and if I define the start of the sequence used in the for loop with a variable instead of a constant I get really weird behavior and there are ...
1
vote
2answers
146 views

How does bash's arithmetic work?

read num1 2 read num2 5 echo "$((num1+num2))" 7 echo "$(($num1+$num2))" 7 I referred this. I don't understand how the above both expressions yields same result? Please explain. echo ...
3
votes
3answers
4k views

Calculate and divide by total with AWK

Given the following data file... foo 10 bar 20 oof 50 rab 20 ... how would I print column two as a percent of the total of column two? In other words, I want... foo 10 10% ...
2
votes
3answers
617 views

Calculate and round numbers up in bash

I'm struggling with a slight variation on: How to calculate by taking first field and How to round floating point numbers in shell? I have a file that looks like (space between columns): 1533 ...
1
vote
3answers
736 views

Evaluation an expression and rounding up to three decimals

I have an expression "5+50*3/20 + (19*2)/7" I need to round it up to 3 decimal places. The answer to this is 17.92857142857143. When I use the script below it is giving me 17.928. The answer should be ...
9
votes
1answer
280 views

Security Implications of using unsanitized data in Shell Arithmetic evaluation

In a comment to a recent question, Stéphane Chazelas mentions that there are security implications to double parentheses arithmetic such as: x=$((1-$x)) on most shells. My Google skills seem to be ...
4
votes
2answers
142 views

How do I convert a string into a number interpreted in certain base in bash script?

I'm simply trying to convert a string $RECORD_HDR to a number $RECORD_SIZE, where echo $RECORD_HDR gives 003D and echo $RECORD_SIZE should give 61
-4
votes
1answer
145 views

Why is setting a variable name giving me “command not found”?

I want to input a number and then add taxes and subtract discounts. I am trying: #!/bin/sh ... final_amount = $(echo "$purchase_amount+($purchase_amount * $tax)-($purchase_amount * $discount)" |bc ...
5
votes
3answers
1k views

How to iterate a zero padded integer in bash?

How does one iterate a string with the form "[A-Z][0-9]*" Or for example: "A000001"? After receiving the variable I split: current_=$(mysql -h"$mysqlhost" -u"$mysqluser" -p"$PASS" "$DBNAME" -se ...
3
votes
3answers
298 views

Get pairs of lines and multiply [closed]

I'm trying to parse an html file for some values using bash. So I've got a big text file with numbers like this 3 2 4 2 What I want to do is get two lines, multiply both numbers and then store the ...
1
vote
3answers
430 views

Add or subtract a number from the names of all the files in a directory

I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory, How can I add 1 to their names, for example, to get 0002.png and 0003.png from 0001.png and 0002.png ...
2
votes
4answers
1k views

How to subtract two times in shell

I nee to subtract two lines which is in the format of time in shell. The time format is hh:mm:ss I used the code below to get the time. cat /var/log/kern.log |grep usb |tail -2| awk '{print $3}' ...
2
votes
1answer
8k views

How to round floating point numbers in shell?

How do I correctly round IEEE 754 floating point numbers on the command line? I want to specify the precision of the output number - the count of fractional digits. Rounding 6.66 to precision 1 ...
4
votes
3answers
353 views

Arithmetic operations with expr and variables

Why won't this statement work? total=`expr $mPercent / 100 "*" .482 + $fPercent / 100 "*" .518` mPercent is a number as a result of an awk statement, as well as fPercent. I also get "expr: ...
1
vote
1answer
387 views

Rounding float numbers in csh

I have a float number stored in a variable $temp, like 3.046789. How we can round this number with 2 digits precision and the result I need is 3.05. The script I have is a csh script.
26
votes
5answers
24k views

Parenthesis in bash arithmetic: 3 * (2 + 1)

expr does not seem to like parenthesis (used in mathematics to explicit operator priority): expr 3 * (2 + 1) bash: syntax error near unexpected token `(' How to express operator priority in bash?
0
votes
4answers
391 views

Bash if conditions of variables within intervals

I have some data spread over time intervals, and I want to take some of those data within the time intervals. For example, I have data at times within 1..9, 11..19, etc., and I want to take data ...
1
vote
4answers
2k views

Error: integer expression expected

I have written a script to determine the Load average on the server as follows: #!/bin/bash loadavg=`top -b -n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'` if [ "${loadavg}" -le 1 ...
2
votes
2answers
4k views

Comparison of decimal numbers in bash

My search this morning was about how could I compare two decimal numbers in bash, and I came to this answser: How to compare to floating point number in a shell script. This one, however, doesn't ...