Subtracting strings (that are numbers) in a shell script
You can do basic integer math in bash by wrapping the expression in $((
and ))
.
$ echo $(( 5 + 8 ))
13
In your specific case, the following works for me:
$ echo "${new_size}-${current_size}"
802-789
$ echo $(( ${new_size}-${current_size} ))
13
Your output at the end is a bit odd. Check that the grep expression actually produces the desired output. If not, you might need to wrap the regular expression in quotation marks.
Subtracting a varibale obtained as output of commands and an integer in shell script
You are getting this error because BASH arithmetic cannot handle floating point numbers and you will get same error while running this command in bash
:
((100-93.0))
But you can skip grep
, sed
and all bash
directives. Just a single awk
can handle this computation like this:
top -bn1 | awk -F, '/Cpu/ {print 100-$4}'
shell script for grabbing data and subtracting
#!/bin/sh
URL1=http://runescape.com/title.ws
tot=`wget -qO- $URL1 | grep -i PlayerCount | cut -d\> -f4 | cut -d\< -f1 | sed -e's/,//'`
URL2=http://oldschool.runescape.com
b=`wget -qO- $URL2| grep "people playing" | awk '{print $4}'`
a=`expr $tot - $b`
echo "$a people `date '+%r %b %d %Y'`"
... if you want commas do this add these line to the script ...
export LC_ALL=en_US.UTF-8
a_with_comma=`echo $a | awk "{printf \"%'d\n\", \\$1}"`
echo "$a_with_comma people `date '+%r %b %d %Y'`"
shell script subtract fields from pairs of lines
Using GNU awk:
awk -F '[ -]' '{ map[$2][$3]=$4;print } END { for (i in map) { print i": "(map[i]["stop:"]-map[i]["start:"])" // ("map[i]["stop:"]"-"-map[i]["start:"]")" } }' file
Explanation:
awk -F '[ -]' '{ # Set the field delimiter to space or "-"
map[$2][$3]=$4; # Create a two dimensional array with the second and third field as indexes and the fourth field as the value
print # Print the line
}
END { for (i in map) {
print i": "(map[i]["stop:"]-map[i]["start:"])" // ("map[i]["stop:"]"-"-map[i]["start:"]")" # Loop through the array and print the data in the required format
}
}' file
Subtracting a number from a variable throwing an error
str=$(grep -w $intron ../file_to_be_grepped_from | awk '{print($2-1)}')
en=$(grep -w $intron ../file_to_be_grepped_from | awk '{print($3+1)}')
Add/subtract variables in a really dumb shell
Another specific solution to your problem (n2 - n1 + 1
) based on seq
, sort -nr
and uniq -u
(POSIX-compliant).
foo()
{
{
seq 1 "$2"
seq 0 "$1"
} \
| sort -n \
| uniq -u \
| grep -n "" \
| sort -nr \
| { read num; echo "${num%:*}"; }
}
$ foo 100 2000
1901
Related Topics
Influxdb Not Asking for Authentication
Cannot Kill Redis-Server on Linux
Implementation of Function Execve (Unistd.H)
Vue Npm Run Serve Failed to Load Resource: Net::Err_Content_Length_Mismatch
Aosp Build Error: Unrecognized Module Type "Hidl_Package_Root"
./Configure-With-Boost No Such File or Directory
Command and Script to Re-Read a File in Gnuplot
Putting Extensions in a Certificate in Openssl
Reducing Privileges of a Process on Unix or Gnu/Linux
How to Use Schell Script to Read Element from a File, Do Some Calculation and Write Back
Rmpi: Cannot Use Mpi_Comm_Spawn API
Compare Two Files of Different Columns and Print Different Columns
Why Does Printf Still Work with Rax Lower Than The Number of Fp Args in Xmm Registers
Truncate Table via Command Line in Linux
Bash Linux - Massive Folder Rename
A Wrong Size of "Len" Calculated by $ - Symbol with Fasm Equ