Operating System - HP-UX
1846819 Members
11131 Online
110256 Solutions
New Discussion

Re: need help with a script

 
SOLVED
Go to solution
S-un-B-ix-S
Advisor

need help with a script

I need help with a script I am trying - I was hoping that someone would have a more elegant solution that what i have come up.

I have 6 files in the following directory -

(named like this)
/tmp/file1
/tmp/file-2
/tmp/file-3
/tmp/file-4
/tmp/file-5
/tmp/file-6

Each file contains the following:

Tue Jun 20 2006 06:30AM EDT 0.66

I need to open each file - grab the last field (0.66) - sum it with all of the numbers out of the other field - take an average- and place that number in a field.

I had done it by using, cat, awk, assigning the number to variable, and then adding all of up and dividing by 6.

I am a beginner at shell scripting so I am sure there is a more "elegant" way to do this, and I would be very interested to see other perspectives.
8 REPLIES 8
Ninad_1
Honored Contributor

Re: need help with a script

I did not quite understand the meaning of " and place that number in a field"
But here is what you can do
for i in file1 file-2 file-3 file-4 file-5 file-6
do
awk '{tot+=$NF} END {print tot/NR}' $i
done

Please let know if you have any other requirement than above

Regards,
Ninad
S-un-B-ix-S
Advisor

Re: need help with a script

Thats is pretty spiffy.

Additionally, I need to average these 6 numbers, and be able to place the average in a file.
Ninad_1
Honored Contributor

Re: need help with a script

I think now I understand a bit of your requirement. Please correct me if I am wrong.
1.You want to the sum of last fields and you want to perform this for each file
2. Then you want to average these sums

What I initially thought was you want average for each individual files.

If I have now understood what you want here it is


sum1=$(awk '{tot+=$NF}' file1)
sum2=$(awk '{tot+=$NF}' file-2)
sum3=$(awk '{tot+=$NF}' file-3)
sum4=$(awk '{tot+=$NF}' file-4)
sum5=$(awk '{tot+=$NF}' file-5)
sum6=$(awk '{tot+=$NF}' file-6)
average=$(echo "($sum1 + $sum2 + $sum3 + $sum4 + $sum5 + $sum6)/6" | bc)
echo $average >> output file

Is this what you require ?

Regards,
Ninad
Kenan Erdey
Honored Contributor

Re: need help with a script

Hi;


top=0
i=0
for f in /tmp/file*
do
xx=`(cat $f | grep EDT | cut -f8 -d' ')`
top=`echo $top + $xx | bc`
i=`expr $i + 1`
done
ort=`(echo $top / $i | bc -l)`
echo $ort > result.out

Computers have lots of memory but no imagination
Sandman!
Honored Contributor
Solution

Re: need help with a script

Here's an elegant one-line awk construct that you could use...

# awk '{sum+=$NF;f++} END{print "average = "sum/f}' /tmp/file-[1-6]

~cheers
Ninad_1
Honored Contributor

Re: need help with a script

Sandman,
I think since the filename for 1st file is without a - there needs to be a bit of modification to your one liner.
Nice one.

Regards,
Ninad
Gordon  Morrison_1
Regular Advisor

Re: need help with a script

Have a look at this thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=824451
What does this button do?
S-un-B-ix-S
Advisor

Re: need help with a script

Thanks guys.