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

28
votes
5answers
37k views

Parenthesis in expr 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?
62
votes
16answers
75k views

Is there a way to get the min, max, median, and average of a list of numbers in a single command?

I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script. Although my immediate situation is for ...
0
votes
1answer
166 views

Why does the factor command produce nonsense on an RSA modulus?

Ok, so I think I may have found a flaw in... something. I honestly don't know. This is such a weird scenario. I was attempting to break very small asymmetric (RSA) encryption, just as a Proof of ...
13
votes
5answers
27k views

Binary to hexadecimal and decimal in a shell script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
0
votes
1answer
51 views

Bash script to add or subtract two numbers from user input

I am very new to Linux and am taking a course in it. Very excited to learn more. I am working on a script that asks user for two numbers, then asks whether user wants to add or subtract. I failed the ...
1
vote
4answers
114 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: ...
0
votes
1answer
56 views

How does this equation gives numbers random between two values?

How does this equation gives random numbers between max and min divisible by divisibleBy echo $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))
10
votes
4answers
2k views

Why does “$(( ~33 ))” produce -34?

$ echo $(( 255 )) 255 $ echo $(( 33 )) 33 $ echo $(( ~33 )) -34 $ echo $(( ~255 )) -256 $ and my kernel is: $ uname -a Linux HOSTNAME 3.2.0-40-generic-pae #64-Ubuntu SMP Mon Mar 25 21:44:41 UTC ...
0
votes
2answers
93 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
votes
1answer
45 views

Arithmetic expressions in for-loop

In for (( expr1 ; expr2 ; expr3 )) ; do commands ; done expr1, expr2, and expr3 are arithmetic expressions. Is expr1 ; expr2 ; expr3 not an arithmetic expression? (( expr1 ; expr2 ; expr3 )) isn'...
0
votes
3answers
78 views

Comparing integers: arithmetic expression or conditional expression

In Bash, two integers can be compared using conditional expression arg1 OP arg2 OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if ...
1
vote
1answer
34 views

Arithmetic expansion and parameter expansion

$ x=foo; foo=bar; bar=6; $ echo $x foo $ echo $((x)) 6 why can the arithmetic expansion trace from variable x to its value foo, from variable foo to its value bar, and then from variable foo to ...
3
votes
1answer
161 views

Variables of integer type in Bash?

There are two ways to define and use variables of integer type in Bash declare -i a new variable use a variable in an arithmetic expression, without declaring it. My questions: What are the ...
-2
votes
1answer
40 views

Is a string an arithmetic expression?

In Bash, I use a string where an arithmetic expression is expected: $ (( hello )) $ echo $? 1 $ echo $(( hello )) 0 $ let aaa=hello $ echo $? 1 $ echo $aaa 0 There is no error reported. So is a ...
2
votes
1answer
28 views

Bash shell script program that prompts for and reads two integers from the user.

Okay, so I been trying to figure this problem for a while and I'm coming up with errors left and right. This is my first time trying to learn the basics. So any advice would be helpful! Problem: I'm ...
0
votes
2answers
37 views

Sed incremental arithmetic

I want to replace a number at the end of a string by incrementing the number by one each time I run a specific sed line. the string looks like this: server-port=25555 I was thinking I could run ...
2
votes
1answer
84 views

Why do newer version of awk print large integers instead of decimals?

I often use the awk (gawk) calculator: calc(){ awk "BEGIN{ print $* }" ;} In Ubuntu 14.04, it output big numbers in standard form, e.g. $ calc 56777654409823*4674990588446667952594759939814064128 ...
3
votes
2answers
140 views

What does the comma operator do in Bash arithmetic?

In Bash Manual, sec 6.5 Shell Arithmetic expr1 , expr2 comma What does the comma operator do? Are expr1 and expr2 arithmetic expressions?
1
vote
2answers
32 views

Does command substitution within arithmetic substitution get word split?

I seem to recall from comments on this site that the contents of arithmetic expansion may be word split, but I can't find the comment again. Consider the following code: printf '%d\n' "$(($(sed -n '/...
15
votes
8answers
29k views

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but ...
0
votes
1answer
30 views

Arithmetic syntax error with a number variable

I am having a difficulty with an arithmetic syntax error. I am reading the name of text files from the command line and count the number of the lines of each file. NUM=$(wc -l "$text") and then I ...
0
votes
1answer
46 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
36 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 ...
4
votes
3answers
47k views

How to add arithmetic variables in a script

I want to accumulate the line size of a number of files contained in a folder. I have written the following script: let a=0 let num=0 for i in folder/* do num=`cat $i | wc -l` a=$a+$...
3
votes
0answers
72 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
50 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 ...
2
votes
4answers
2k 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}' ...
14
votes
4answers
7k views

how to suppress bash octal number interpretation? (to be interpreted as decimal)

I have this: date +"%H hours and %M minutes" I use festival to say it up.. but it says like: "zero nine hours".. I want it to say "nine hours"! but date always give me 09... so I wonder if bash ...
0
votes
1answer
100 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 result_1=a(1)*100/b(...
4
votes
1answer
5k views

awk high precision arithmetic

I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. ...
1
vote
1answer
83 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
134 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 ...
3
votes
3answers
7k views

case + how to implement equal or less or greater in case syntax

My target is to verify a range of number with (only with case + esac), and print the range. So for example: If the number is between 0 and 80, print >=0<=80 If the number is between 81 and 100 ...
12
votes
4answers
14k views

creating a sequence of numbers, one per line in a file

Is there a way to create out of thin air, a file that is a sequence of numbers, starting at a given number, one per line? something like magic_command start 100 lines 5 > b.txt and then, b.txt ...
1
vote
1answer
45 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 ...
0
votes
2answers
171 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
43 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 ...
3
votes
3answers
16k views

How to calculate values in a shell script?

I run this command in the terminal: grep "bla bla blah" blah* | echo "Blah: $(wc -l) / $(ls | wc -l) * 100" And I get this output: Blah: 44 / 89 * 100 What I expect to see: 49.4 Is there a ...
3
votes
2answers
539 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 #!/bin/...
1
vote
2answers
350 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 ...
16
votes
8answers
29k views

How to compare to floating point number in a shell script

I want to compare two floating point numbers in a shell script. The following code is not working: #!/bin/bash min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min
6
votes
2answers
505 views

Trying to do arithmetic in Perl rename tool (Debian)

Let's suppose I have five mp3 files: 01-trackfoo.mp3 02-trackbar.mp3 03-trackbaz.mp3 04-trackabc.mp3 05-trackxyz.mp3 Now I give a quick listen to the files (just mplayer on console will do the ...
1
vote
1answer
83 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
128 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
367 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 ...
-4
votes
1answer
377 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 -...
25
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 /usr/share/man/man1/bash.1....
3
votes
2answers
5k 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 ...
0
votes
4answers
315 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 ...