Translate bash mathematical calculation to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Translate bash mathematical calculation to awk
# 1  
Old 08-30-2014
Translate bash mathematical calculation to awk

this code below is very useful in calculating mean and quartiles. however, i would really like to translate it to awk without having to write to any external file:
Code:
#!/bin/sh

filename="tmp.txt"

sort -n $1 >$filename

rows=`wc -l $filename|cut -d' ' -f1`
q2=`echo "($rows+1)/2" |bc`

q1=`echo "$q2 / 2"|bc`
q3=`echo "3 * $q1" |bc`
echo  "Q1=  " `head -$q1 $filename|tail -1`
echo  "Q2= "`head -$q2 $filename|tail -1`
echo  "Q3= "`head -$q3 $filename|tail -1`

is this possible?

content of file which will be passed to $1:

Code:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 
2 2 2 2 2 2 2 2 2 3 4 4 5 30 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 
36 37 37 37 37 37 37 37 37 38 38 38 38 38 38 38 38 38 38 38 39 39 39 39 39 39 39 39 40 44 668 767 792
 795 803 805 805 805 805 805 805 805 806 806 806 806 806 806 806 806 807 807 807 808 863 868 875 883
 884 903 910 912 923 934 952 954 968 971 973 983 999 1007 1008 1017 1022 1044 1060 1125 6383 7275 9614 9629

# 2  
Old 08-31-2014
What output are you expecting from this input?

What OS are you using?

If you want a bash script, why are you invoking /bin/sh instead of /bin/bash to interpret your script?

Why are you sorting a sorted 7 line file?
# 3  
Old 08-31-2014
Quote:
Originally Posted by Don Cragun
What output are you expecting from this input?

What OS are you using?

If you want a bash script, why are you invoking /bin/sh instead of /bin/bash to interpret your script?

Why are you sorting a sorted 7 line file?
i only want a awk solution. i found the bash script posted in this thread online when i was looking for a solution on how to identify outliers in a range of numbers, which i posted above.

i figured the logic is there in that bash script, i just need to translate that to awk.
# 4  
Old 08-31-2014
It could certainly be reworked in to better shell scripting to not need an intermediate file.
Reworking it in just plain awk is difficult since awk doesn't have it's own sort function.
How about this?

Code:
mute@thedoctor:~$ cat script
#!/bin/sh
tr -s ' \t\n' '\n' < "$1" | sort -n | awk '
  { a[NR]=$1 }
  END {
    print "Q1=" a[int(NR/4)]
    print "Q2=" a[int(NR/2)]
    print "Q3=" a[int(3*NR/4)]
}'

mute@thedoctor:~$ ./script file
Q1=1
Q2=36
Q3=38

From what I read about quartiles, this should be expanded to take the average of two values if the quarter falls between two. For this data set the answers match.
This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

How To Perform Mathematical Operation Within If in awk?

Hi All, I am using an awk script as below: awk -F'|' 'BEGIN{OFS="|";} { if ($1==$3 && $3==$7 && $7==$13 && $2==$6 && $6==$11 && $15-$14+1==$11) print $0"|""TRUE"; else print $0"|""FALSE"; }' tempfile.txt In above script, all conditions are being checked except the one which is... (4 Replies)
Discussion started by: angshuman
4 Replies

3. Shell Programming and Scripting

Translate grep to awk

sed -n "2,10p" lfile | egrep error | egrep -vc memory sed -n "2,10p" lfile | egrep error | egrep -v memory sed -n "2,10p" lfile | egrep error | egrep -c memory sed -n "2,10p" lfile | egrep error | egrep memory above are four separate commands. i want to combine the grep in each... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

translate sed to awk

hi, can someone tell me how can I translate the following line from sed to awk? `sed 's/^*:*:*:*:*:\(*\):.*/\1/ How to use code tags when posting data and code samples. (14 Replies)
Discussion started by: adam25bc
14 Replies

5. Shell Programming and Scripting

Can BASH handle mathematical operations and do a Search & Replace?

Hello, I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it. I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers... (12 Replies)
Discussion started by: jl487
12 Replies

6. Shell Programming and Scripting

translate a short csh script to bash

Hi, I have a csh: set NODES = `cat $HOST_FILE` set NODELIST = $TMPDIR/namd2.nodelist echo group main >! $NODELIST foreach node ( $NODES ) echo host $node >> $NODELIST end @ NUMPROCS = 2 * $#NODES I am very frustrated to translate it to bash: NODES = `cat... (3 Replies)
Discussion started by: rockytodd
3 Replies

7. Shell Programming and Scripting

Error in mathematical calculation using bc

I am getting the error: Runtime error (func=(main), adr=10): Divide by zero When executing the mathematical expression: echo "scale=2; 1-(0/0)"|bc How to overcome this? (5 Replies)
Discussion started by: proactiveaditya
5 Replies

8. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 Replies

9. UNIX for Dummies Questions & Answers

use a mathematical expression in an awk statement

I have some geometric data (X Y) that is in the wrong scale. My raw data is in mills but it needs to be in tenths of a mil I am pretty familiar with awk and sed I want to use awk to divide $1 and $2 by .1 I'm just not sure of the syntax $1 and $2 are variables and .1 is fixed simple... (3 Replies)
Discussion started by: awk_sed_hello
3 Replies

10. Shell Programming and Scripting

awk calculation

Hallo all, I have a script which creates an output ... see below: root@a7germ:/tmp/pax > cat 20061117.txt 523.047 521.273 521.034 517.367 516.553 517.793 513.114 513.940 I would like to use awk to calculate the (a)total sum of the numbers (b) The average of the numbers. Please... (4 Replies)
Discussion started by: kekanap
4 Replies
Login or Register to Ask a Question