Arithmetic with bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arithmetic with bash
# 1  
Old 11-11-2019
Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using:

Code:
tr -cd [:space:] < afile | wc -c

I am also able to get the total number of characters using:
Code:
wc -c afile

How do I divide the first number by second to get the final answer.
# 2  
Old 11-11-2019
My personal first step is always to consult the relevant man pages. bash provides "arithmetic expansion" / "arithmetic evaluation". man bash:
Quote:
Arithmetic Expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$((expression))
Quote:
ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, . . . is done in fixed-width integers with no check for overflow, though division by 0 is trapped and
flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language.


As it provides integer calculations only, we need additional measures as your data will be percentages and never exceed 1. You'll need "command substitution" as well. So - try
Code:
echo $(($(tr -cd [:space:] < afile | wc -c)00 / $(wc -c < afile)))

The two zero characters are in fact a multiplikation by 100. Be aware that [:space:] includes <TAB>s and <LF>s as well.
# 3  
Old 11-11-2019
bash does integer arithmetics only.
Code:
awk '{tot+=length+1; spc+=gsub(/[[:blank:]]/,"")+1} END{print spc/tot}' afile

The +1 takes the newline character into account like the previous wc.

Last edited by MadeInGermany; 11-11-2019 at 05:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arithmetic issue

I found the following issue by simply increasing a variable. The ((A++)) expression returns an error, the other expressions both return 0. Does anyone know why? script.sh: #! /bin/bash A=0 B=0 C=0 ((A++)) ; echo "${?}" ((B=B+1)) ; echo "${?}" ((C+=1))... (8 Replies)
Discussion started by: elbrand
8 Replies

2. Shell Programming and Scripting

Arithmetic on a Float in bash

I am using bash I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows: number=85.152 A=${number%.*} #Converts float to integer typeset -i B=$(((A-20)/10)) #subtracting 20 is... (12 Replies)
Discussion started by: prodigious8
12 Replies

3. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

4. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

5. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

6. Shell Programming and Scripting

Need help is manipulating a file with some arithmetic operations using bash script

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 Now I need to manipulate the file in such a way that to all the numbers less than 163, 63 gets added and to all numbers greater than 163, 63 gets deducted.(The numbers... (2 Replies)
Discussion started by: shrijith1
2 Replies

7. Shell Programming and Scripting

Arithmetic on timestamps

Hi Friends, please advise on shell script to add two time stamps for example : a=12:32 b=12:00 c=a+b=00:32 please help me to find shell script to add to two time stamps, as i need to convert time from EST to GMT or SST to prepare status of jobs in unix and to specify estimated time to... (3 Replies)
Discussion started by: balireddy_77
3 Replies

8. Shell Programming and Scripting

arithmetic in tcsh

Yes I know tcsh sucks for scripting and arithmetic but I have to write a script for multiple users and they all use tcsh. I have this variable that I 'set' with but pulling numbers off of stings with set STUFF = `grep string file | awk command` Now I would like to add up the numbers that... (4 Replies)
Discussion started by: gobi
4 Replies

9. UNIX for Dummies Questions & Answers

arithmetic syntax

okay, I'm a complete beginner, and I keep getting stuck on the syntax here. I want to write a script where I read the current time as minutes and seconds, convert the minutes to seconds, and add the two, then redirect the output to a file. the command takes two arguments, which will also be... (1 Reply)
Discussion started by: paprbagprincess
1 Replies

10. UNIX for Advanced & Expert Users

time arithmetic

Can anyone help please. I am writing a kourne shell script and I am unsure how to do the following: I have extracted a time string from a logfile, and I have another time string I want to compare it to to see if it's later than the time I'm comparing with. i.e. expectedSLA="23:00:00", ... (2 Replies)
Discussion started by: csong2
2 Replies
Login or Register to Ask a Question