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

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}' ...
2
votes
4answers
2k views

how to convert number to time format in shell script?

I want to cut a video into about 10 minute parts like this. ffmpeg -i video.mp4 -ss 00:00:00 -t 00:10:00 -c copy 01.mp4 ffmpeg -i video.mp4 -ss 00:10:00 -t 00:10:00 -c copy 02.mp4 ffmpeg -i video.mp4 ...
2
votes
1answer
130 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 ...
2
votes
1answer
10k views

Divide a variable by a number? [duplicate]

Basically I need to convert centimetres to inches which I am trying to do by diving the area in centimetres by 2.54. But I just cannot get this to work. echo "please enter width and then height" ...
2
votes
3answers
148 views

Change value of a strings representing number

I have the following expression : a="2013" How can I change the value of a to "2012".
2
votes
2answers
3k views

How to divide a list of values by a number in command line?

I am trying to translate a simple program to the command line using unix utilities. For example, if I have a frequency list (after piping through uniq and sort) 5 x 4 y 1 z I want to print out, ...
2
votes
3answers
5k views

grepping a variable and adding 1 to it

I have a variable containing a leading zeroes number, and I want to grep this variable and that same variable plus one. I made several attempts but ran into errors. Here's what I want to do: read var ...
2
votes
1answer
77 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 ...
2
votes
1answer
363 views

Difference between two types of shell arithmetic

What is the difference between the following two shell arithmetic commands: echo $[ $var1 - 1 ] echo $(( $var1 - 1 )) Assuming var1 = 5 for example.
2
votes
1answer
27 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 ...
2
votes
2answers
110 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 ...
2
votes
1answer
199 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 ...
2
votes
3answers
724 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 ...
2
votes
2answers
352 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 ...
1
vote
3answers
6k views

Shell scripting to divide numbers inside a file

I have a single column and 12 rows. Each row has numerical values. I want to divide each row values by some constant number (say C) using shell (bash) scripting. How to do this ? Thanks in advance..
1
vote
4answers
3k 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 ...
1
vote
3answers
1k views

Writing a shell script to take output of an executable and perform some calculation

I am not much familiar with shell script. I want to write a shell script for the following pseudo-code: min=some garbage value for(i=1 to N){ // N and n will be taken as input for the shell script. ...
1
vote
3answers
532 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 ...
1
vote
4answers
111 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: ...
1
vote
2answers
31 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 ...
1
vote
1answer
1k 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 ...
1
vote
2answers
163 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 ...
1
vote
3answers
854 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 ...
1
vote
1answer
112 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 ...
1
vote
2answers
520 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 ...
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 ...
1
vote
2answers
120 views

modify user-defined System Variables(that is a number) of remote Server machine using ssh?

I want to modify the value of a System Variable of a remote server . The IP of server is 172.16.22.53. The server runs Windows OS,and Cygwin instlled in it.In ...
1
vote
1answer
32 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 ...
1
vote
1answer
70 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 --- ...
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 ...
1
vote
1answer
149 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 ...
1
vote
2answers
2k 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
514 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.
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 ...
1
vote
2answers
310 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
79 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
42 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 ...
0
votes
2answers
127 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
4answers
442 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 ...
0
votes
1answer
25 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
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))
0
votes
2answers
88 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, ...
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.
0
votes
3answers
63 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 ...
0
votes
2answers
36 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 ...
0
votes
1answer
80 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 ...
0
votes
4answers
285 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 ...
0
votes
2answers
193 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
42 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 ...