Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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..

share|improve this question
martin@dogmeat ~ % cat test 
martin@dogmeat ~ % cat test | while read i; do echo "$i/2" | bc; done
martin@dogmeat ~ % cat test | while read i; do echo "scale = 5; $i/2.0" | bc; done

bc doesn't have a rounding function, so if you want to have those numbers properly rounded you will probably have to implement the function yourself.

share|improve this answer
You can use printf to do the rounding for you – glenn jackman Sep 23 '13 at 18:15
I would use done < test to avoid the UUoC. – Joseph R. Sep 23 '13 at 18:16
the trouble with caring about UUoC is that in cat is often just a placeholder for any other command or pipeline that produces output. – cas Sep 24 '13 at 0:27

If you're OK with integer division:

while read num; do
    echo $(( num / c ))
done < file

otherwise, you can use

awk -v c=3 '{ print $1/c }' file
share|improve this answer

To get around the intricacies of shell floating point arithmetic, why not use Perl? Here's a one-liner to do it:

C=3 perl -ne 'printf "%.2f\n",$_/$ENV{C}' your_file

The gargantuan answer to this question may also be interesting to you.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.