Questions tagged [arithmetic]

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.

Filter by
Sorted by
Tagged with
2 votes
2 answers
114 views

Replicating floating point arithmetic WITHOUT bc utility

I'm needing to write a script on some embedded Linux systems that don't have bc installed on them and won't be able to have it installed. The script I'm writing is essentially a monitoring script that ...
user avatar
  • 23
2 votes
3 answers
118 views

Performing calculation on the contents of two files

file1 contains 100 as its content, and file2 contains 200. How would I perform calculation on those two numbers in the terminal? Say I would like to perform division on those two numbers. My approach ...
user avatar
0 votes
0 answers
31 views

How to replace text in file - conditions and arithmetic required

I need a script to change content in some text files. This a bit beyond my limited bash skills. Maybe perl or python would be a better approach, I have no experience with either of these. I have a ...
user avatar
11 votes
2 answers
4k views

bash calc returns 800 (should be 600): echo "$(( 800 / (4 / 3) ))"

I'm calculating aspect ratio height from x number, in this example I'm using 4:3 ratio, and a width of 800, the result (height) should be 600, but bash is returning 800, and I'm not sure why. I've ...
user avatar
  • 265
1 vote
1 answer
60 views

Sum of timestamps in unix [closed]

I need a code to add 2 timestamps of format HH:MM:SS in unix, can anybody help? Unfortunately, I have stored timestamp as varchar in the database, in the format (e.g.) 07:53:39 Now I want sum of that ...
user avatar
0 votes
1 answer
74 views

sed giving invalid arithmetic operator error

sed keeps giving invalid arithmetic operator error. I am trying to assign the output of sed to a variable. This one works, var=$(sed -e 's/"currentGeneration":5010/"currentGeneration&...
user avatar
  • 25
0 votes
1 answer
108 views

How to round to the correct nearest number in Linux bc?

How do we round to the correct nearest number in a Linux basic calculator BC?
user avatar
2 votes
4 answers
326 views

Multiply numbers between two different patterns and print the whole file

I would like to replace number between patterns with multiplicated numbers and print the all the lines. The file is a tree file in newick format and consisted only a single line. My targets are all ...
user avatar
  • 187
7 votes
3 answers
1k views

Handling 64-bit integers in a shell script

I am trying to calculate the used bandwidth on the Ethernet interface (which is 1000 Mbit/s). To test my script, I am using the iperf tool to generate huge bandwidths. The problem I am facing is when ...
user avatar
  • 63
0 votes
3 answers
116 views

Is parameter expansion in test expression possible?

When I try the following expression in bash I get a strange error message: [: -lt: unary operator expected First the function definition some_func () { (( 3 + 5 )) } And the expression [ $(...
user avatar
  • 303
3 votes
1 answer
279 views

zsh arithmetic comparison giving false positive

Why is passing 0 as an argument results in a false positive (prints "True")? #!/bin/zsh k="$1" if ((0 < k < 1)) then echo "True" fi Note this script is ...
user avatar
1 vote
2 answers
132 views

How bash interprets triple parentheses?

I see that in bash the command echo $(((i=18))) prints 18. This makes me understand that $(((i=18))) is interpreted as an arithmetic expansion (with the variable i being initialized inside the ...
user avatar
9 votes
4 answers
2k views

Bitwise operations in Bash not working as expected

I ran into a strange problem. To demonstrate, let's take the largest unsigned number on my machine (printf "%X \n" -1 gives me FFFFFFFFFFFFFFFF), and try to shift some bits.  First, shift ...
user avatar
  • 1,620
11 votes
3 answers
1k views

Find the largest number that Bash arithmetic can handle?

How can I let my script determine the largest number for itself? I looked through my environment variables, and I found these two that looked promising: ~# declare -p BASH_VERSINFO HOSTTYPE declare -...
user avatar
1 vote
2 answers
113 views

Arithmetic between 2 files generating a series of new files using awk

I have a tab separated model input file i would like to vary for ensemble analysis formatted such as this cat input.txt ############################################# ### Parameter file for the ...
user avatar
0 votes
1 answer
49 views

Arithmetic between 2 files generating a series of new files (Pt 3)

I have a tab separated model input file I would like to vary for an ensemble analysis formatted similar to this cat input.txt /* Precipitation in mm */ 10 30 15 20 ...
user avatar
0 votes
1 answer
47 views

Arithmetic between 2 files generating a series of new files (Pt 2)

This is a more specific follow-up to a previous question (Arithmetic between 2 files generating a series of new files). I have a tab separated model input file I would like to vary for an ensemble ...
user avatar
2 votes
6 answers
114 views

Arithmetic between 2 files generating a series of new files

I have a tab separated model input file I would like to vary for an ensemble analysis formatted similar to this input.txt /* Preciptation in mm */ 10 30 40 50 23 ### Species description *** sp_name ...
user avatar
0 votes
2 answers
45 views

addition by specific value of arithmetic values presented in file1 to file2

I have in file 1 the following 'a 62-182' 'a 183-303' 'a 304-424' ... is there any way I can have based on file1's value the following in file 2? 'a 62-182' 'a 7195-7315' 'a 183-303' 'a 7316-7436' ...
user avatar
4 votes
1 answer
120 views

In ZSH, what arithmetic expressions can appear as array subscript?

The ZSH manual (zshparam(1)) reads: Array Subscripts Individual elements of an array may be selected using a subscript. A subscript of the form `[exp]' selects the single element exp, ...
user avatar
  • 734
0 votes
3 answers
911 views

Divide by zero exception using awk

i have values stored in variables var1=6 and var2=4, i am trying to divide them with a precision of 2 and store the result in a variable var, var=`awk 'BEGIN {printf "%.2f\n", $var1/ $var2}'`...
user avatar
0 votes
0 answers
16 views

BASH: arithmetic expressions on point values [duplicate]

I am looking for bash function suitable to do some simple arithmetic expressions on point values For intance i=1 let "i_mod=${i}+1"; echo $i_mod gives me 2 but if i=1.0 let "i_mod=${i}+...
user avatar
9 votes
5 answers
655 views

Arithmetic on values with memory size units

Let’s say I have a bunch of numbers representing quantities of memory, written in the form 86k or 320m or 1.7g for instance. How can I compute their sum in command line, and get back a human-readable ...
user avatar
  • 386
1 vote
3 answers
183 views

How do I sum up the results of multiple bash commands in bash?

So I've got the following cat a1 | wc -l // term1 cat a2 | wc -l // term2 cat a3 | wc -l // term3 ... cat a10 | wc -l // term10 And I'd like to calculate term1 + term2 + ... + term10 in a one liner (...
user avatar
  • 147
0 votes
2 answers
227 views

Rounding numbers of a CSV file, skipping the header

This question is a derivative of the https://askubuntu.com/questions/601149/is-there-a-command-to-round-decimal-numbers-in-txt-files, which was successfully solved by using: perl -i -pe 's/(\d*\.\d*)/...
user avatar
  • 93
-3 votes
3 answers
91 views

Bash script - How is 6 > 50? [duplicate]

if [[ 6 > 50 ]]; then echo "true" fi $ bash script.sh I'm missing something very obvious here. Why is 6 greater than 50 ?? ** EDIT ** I'm also try to solve for if [[ 6.5 > 50 ]]; ...
user avatar
  • 201
6 votes
4 answers
2k views

Why does the command a-=2 fail?

After executing the command declare -i a=5, the command a+=2 succeeds, but the command a-=2 fails. Can someone explain this strange behavior of bash?
user avatar
  • 705
0 votes
1 answer
229 views

Sum all media durations in current folder

I had a bunch of media (videos to be precise) in a folder and I wanted to sum all the lenghts. I'll be ignoring seconds and hours for now, just to get the gist of it. I wrote this script: sum=0 for ...
user avatar
1 vote
2 answers
703 views

Sum of large numbers and print the result with all decimal points for the stated question when using awk arrays

I have a below input file which I need to split into multiple files based on the date in 3rd column. Basically all the same dated transactions should be splitted into particular dated file. Post ...
user avatar
  • 83
2 votes
4 answers
2k views

Sum all positive integers in a bash array

How can I sum all positive integers in a bash array. Here is my code: #!/bin/bash arr=(2 43 -1 -33 24 12 -6) for (( i = 0; i < ${#arr[@]}; ++i )); do if (( arr[i] > 0 )); then sum=`...
user avatar
0 votes
2 answers
647 views

awk column-wise division of all lines by another line

I'm trying to divide all lines in file1.txt by their respective (column-wise) value in the single line in file2.txt. cat file1.txt 1 2.5 3 7 7 7 1 3 5 cat file2....
user avatar
1 vote
2 answers
216 views

Bash: Arithmetic expansion, parameter expansion, and the comma operator

I have a question about bash's parameter expansion, inside a comma expression, inside an arithmetic expression. I have two statements that I thought should be equivalent, but they're not. Why does ...
user avatar
0 votes
2 answers
1k views

How to resolve Illegal number: -1.00 | ubuntu

I have a scenario where I am taking difference of two numbers. Then the calculated difference needs to be checked within a range. If it lie within that range then display status as success, else ...
user avatar
5 votes
1 answer
1k views

Why does arithmetic expansion work in Bash but not in Zsh?

I was demonstrating a piece of funny code (or so I think) to my students in class, and to my surprise, it did not work in zsh. I then switched to bash and it worked as expected. The code is the ...
user avatar
  • 169
5 votes
4 answers
1k views

How to get bc to print trailing zeros?

I read topics about how to get bc to print the first zero, but that is not exactly what I want. I want more... I want a function that returns floating point numbers with eight decimal digits. I am ...
user avatar
3 votes
2 answers
806 views

Why we can not compare . (dot) as a string value inside the round brackets of if statement?

Problem I have to compare a string "problem1.sh" with itself. It works fine in case of solution1.sh (given below) where I have used square brackets for comparison. However, it does not work in case ...
user avatar
13 votes
4 answers
3k views

How to increment the value of a (decimal) variable (with leading zero) by +1?

I have a file in the name of Build.number with the content value 012 which I need to increment by +1. So, I tried this BN=$($cat Build.number) BN=$(($BN+1)) echo $BN >Build.number but here I am ...
user avatar
  • 193
1 vote
1 answer
123 views

Raise each element of an array to a power and store output to a new array in bash

I want to raise a fraction (fraction is calculated on first loop) to a decimal power (second loop), however, I always get 1 as a result. I want to store the output of the second loop in an array as ...
user avatar
4 votes
7 answers
5k views

bash - sum numbers in a variable

I have a variable set with numbers separated with space, of which first number can also be led by space, example: VAR=" 2 1 34 3 2 " I need to sum all these numbers. Easiest way would be ...
user avatar
  • 2,627
2 votes
1 answer
124 views

Gnuplot Precission

Does anyone know whether there is a way to run gnuplot with quadruple arithmetic precision? E.g. When you want to plot a line and you want to subtract a value like "2....
user avatar
0 votes
1 answer
75 views

calculate the total md5 of specific match files

in folder , we have the following HADOOP binary files and their size (BYTES) du -sb * | grep HADOOP[a-z] 334542327 HADOOPaa 334542327 HADOOPab 334542327 HADOOPac 334542327 ...
user avatar
  • 11k
3 votes
2 answers
208 views

groff arithmetic

Is it possible to perform arithmetic using groff? Say I want to calculate what page is the current one, and I currently am on page 2. I can get the next page's number by using: .PP Next page is ...
user avatar
  • 167
1 vote
1 answer
183 views

divide by 1000 column 2 to column 1000

How can i divide as fast as possible column 2 to column 1000 by 1000? awk '{printf $"{2..1000..1}"/1000}' file 1 > file2
user avatar
1 vote
1 answer
472 views

How do I return octal from Bash's arithmetic expansion?

As clearly spelled out in the Bash documentation on shell arithmetic, if you prefix a number with a 0 within an arithmetic expansion expression (e.g., $(( expr ))), it's treated as octal. Similarly, ...
user avatar
2 votes
1 answer
2k views

How to sum each column and print column name and column sum using awk?

I have a file with a table of numbers; the first line is a header line. I want to sum each column and print out sum and column name for each column using awk. How can I do it?
user avatar
  • 141
3 votes
4 answers
3k views

How to sum variables from file in Bash [duplicate]

I need to sum numbers from a file line by line. The file: 1.0 0.46 0.67 I want to sum, then divide 3. I currently have: while IFS= read -r var do x=$(($var + $x)) | bc -l done < "file.txt" ...
user avatar
  • 147
0 votes
2 answers
287 views

Add three dot floating numbers in shell

I am trying to append 5.34.03 number to 5.34.04 by using the following command $ awk '{print 5.34.03 + 0.0.1}' 5.340.030.1 Expected output 5.34.13 I tried with many methods, eg. let, but it didn't ...
user avatar
  • 531
0 votes
0 answers
35 views

Any way to interpret string representing an integer or operation?

Lets say I have this string one plus two Is there any way we can interpret this as 1 + 2 faster than going into some conditions checking word per word and testing it if it equal something in a given ...
user avatar
3 votes
2 answers
25k views

String to integer in Shell

I'm currently working on a project called 'dmCLI' which means 'download manager command line interface'. I'm using curl to multi-part download a file. And I'm having trouble with my code. I can't ...
user avatar
0 votes
2 answers
255 views

When I add 0 to 0.0, how do I make sure the result is 0.0 not 0?

There are two columns of numbers in a file, the first line like this: 0 0.0 I want to add the numbers in column 1 to those in column 2, and I want to keep the results floats, not ...
user avatar
  • 27