Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 10-31-2008, 03:01 PM   #1
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
simple bash script problem with division.

Hi all. This is a simple bash script that i've spent way too much time on, I'm tying to get a few stats from a group of files. Basically there is a group of files per day (i.e. today = 20081031-0.txt to 20081031-9.txt) and they vary, but I want to check these logs to see how many times we are hitting a .com, .net, etc.. The "percent" is where I'm having issues... It's not really a big deal, but I figured I'd post it. This will be run on FreeBSD btw, if it matters.
Thanks in advance!

#   Parse daily logs for .com, .net, .org (to start) and push to stats.csv

year=`date +%Y`
month=`date +%m`
day=`date +%d`
day=30     # I did this because the logs for today aren't done.

if [ -e /usr/local/$year$month$day-0.txt ]; then

    total=`cat /usr/local/$year$month$day-?.txt | wc -l | sed 's/ //g'`

    echo "" > stats.csv
    for i in ".com" ".net" ".org";
    do 			# had to use sed to strip the whitespace in hopes it would work...
        count=`cat /usr/local/$year$month$day-?.txt | grep -i $i | wc -l | sed 's/ //g'`
        #percent=$(($count/$total))    # everything else seems to work, but this!
        let "percent = $count / $total"
        echo "$i,$count,$percent,$total >> stats.csv"
        #echo "$i,$count,$percent" >> stats.csv
    echo "$year$month$day-0.txt not found in /usr/local/"
The desired result (for example):
Old 10-31-2008, 03:08 PM   #2
LQ Newbie
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0

If you want fraction then use "bc":

percent=`echo $count*100/$total | bc -l`
Old 10-31-2008, 03:10 PM   #3
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
bash's internal maths can't do floating point calculations; you're best off using bc, which can to a specified number of digits.

edit: too slow by half!


bash, sed, stats

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash simple math (division) question babag Programming 3 04-03-2008 05:24 PM
Simple bash script programming problem ArthurHuang Programming 3 01-28-2008 10:53 AM
Idiotically simple bash script problem. morrolan Programming 7 01-14-2006 05:09 PM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM
problem with a simple script bash stockerz Linux - Newbie 1 09-21-2004 11:42 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:31 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration