I can't divide properly in shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers I can't divide properly in shell
# 1  
Old 02-26-2008
I can't divide properly in shell

I'm new at this and frustratd because I know this must be simple but here goes. I'm trying to write a script that will give me the percentage of a particular field over multiple text files.

Each day I have around 1500 files that are generated. Each file has around 100 lines in it. The lines that I want have LOI in the first field. Each line has 13 fields. The last field either has 0001 (good) or 0002 (bad). I need to find the total lines with LOI and figure the percentage of 0002 of the total.

Getting the total is easy.

total=`grep LOI date* | cut -d" " -f 13 | wc -l`

and getting the bad...

bad=`grep LOI date* | cut -d" " -f 13 |grep 0002| wc -l`

date* gives me all the files for the day in question. So that counts all the records and gets them into a variables. Then I tried

expr $failed / $played

and it just gives me 0.

I then tried this in AWK. I can do this

gawk '/LOI/ {print $13 }' date* | wc -l | gawk '{print "total " $0}'

but can't figure out how to get both values into awk and manipulate them.
please help...
# 2  
Old 02-26-2008
Has to be awk? Well, then maybe...
Code:
$ total=92
$ some=13
$
$ awk -vsome=$some -vtotal=$total 'BEGIN { print some*100/total ; exit } '
14.1304

# 3  
Old 02-26-2008
Tools Not exactly sure of all your variables

Perhaps this example will help you:

Code:
> cat divide
#! /bin/bash

failed=21
played=48

ans=$(($failed * 100 / $played))
echo $failed" / "$played" ="
echo $ans"%"

Quote:
> divide
21 / 48 =
43%
# 4  
Old 02-26-2008
Computer

You guys are great. I subsituted some stuff and here is what I got

total=`grep LOI 1*.* | cut -d" " -f 13 | wc -l`
echo "$total"
failed=`grep LOI 1*.* | cut -d" " -f 13 |grep 0002| wc -l`
echo "$failed"
awk -vsome=$failed -vtotal=$total 'BEGIN { print 100-failed/total*100 ; exit } '

And it works great. Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To divide file

We have large log files, and we need to extract last four hours lines only, Please let us know the command. Thanks, Saurau. (1 Reply)
Discussion started by: saurau
1 Replies

2. Shell Programming and Scripting

Divide by zero script

Hi, I've been working on a few scripts and have been getting great info. How, would I include in the below script, how I would let a user know that if they divide a SECOND number by zero, that they would get a divide by zero error? What's the easiest way of working this? Cordially, joe. ... (1 Reply)
Discussion started by: jefferj54
1 Replies

3. Shell Programming and Scripting

Divide two variable

F= `expr $E/$S` output test5.sh: line 45: 1296863.27001857757568359375/21997: No such file or directory can any one help me ,i just want to divide the $E/$S and Print o/p in F. (3 Replies)
Discussion started by: netdbaind
3 Replies

4. Shell Programming and Scripting

Can't get shell parameters to pass properly to sqlplus

Gurus, The issue I'm having is that my Shell won't accept SQL parameters properly...... Here's they way I'm running it.... applmgr@ga006hds => sh CW_MigrationDeployScript.sh apps <appspwd> <SID> '01-JAN' '31-MAR' The process just hangs not submitting the SQL job... ... (3 Replies)
Discussion started by: WhoDatWhoDer
3 Replies

5. Shell Programming and Scripting

Shell script not working properly

Hello, i have below shell script to process ftp get from server and create file list afte finish. this shell scipt has 6 parameter input. unfortunately, it is only working to get the file and terminated before creating file list. please help. thanks, #!/bin/ksh ## example usage :... (3 Replies)
Discussion started by: daryatmo
3 Replies

6. Shell Programming and Scripting

Shell Script Email not working Properly

Hi GURU's, I'm using a Shell Script to send email's as an attachment. I'm Storing the email address in a table and catching in a variable. MAILLIST=`noarg sqlplus -s $OraUsr << EOF set heading off set feedback off set echo off SELECT email_ids FROM tpemail_table WHERE... (9 Replies)
Discussion started by: karthikraj
9 Replies

7. UNIX for Dummies Questions & Answers

Issue with shell script: not detecting file properly

The following script is meant to check the presence of a file - called filename0.94.tar.gz - and uncompress it: #!/bin/sh # check presence of file VERSION=0.94 if ; then # file not present: abort echo "Files cannot be found." #exit 1 (commented out this line, so we can see how the... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

Output in my shell isn't showing properly.

Hi! Can anyone tell me what went wrong in my shell script? for dt_val in `cut -f 1 -d '|' /prod/ods/satyaki/sqlldr/grp.dat` do echo $dt_val done And, the output is - 23 39 (7 Replies)
Discussion started by: satyakide
7 Replies

9. Shell Programming and Scripting

Shell script not processing if statement properly

Hi I am trying to create a shell script that will look for a contracthead file first and if the contract head file does not exist on day1 exit script. Now on day2 if contracthead exists or not run the script uploading files in order such as contract line then contract contact so the... (2 Replies)
Discussion started by: jonathan184
2 Replies

10. Shell Programming and Scripting

script needs to divide by 2

i need to divide this count by 2, what variable can i use in my script? 26 hcscprod_cpus_totals /2 = 13 13 hcncprod_cpus_totals /2= 6.5 541 ktazp_cpus_totals /2= 270.5 346 ktazd_cpus_totals /2=173 110 ktazi_cpus_totals /2=55 10 ktazq_cpus_totals /2=5 (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question