awk doubt

awk doubt

am 16.01.2008 18:42:17 von apogeusistemas

Hi:
How could I sum all data from column 5 in this file and divide by
numbers of samples?

cat file

7:00:00 4 1 0 95
08:00:00 9 2 0 89
09:00:00 10 2 0 87
10:00:00 15 2 0 83
11:00:00 18 2 1 79
12:00:00 13 2 0 84
13:00:01 4 1 0 94
14:00:01 12 2 1 85
15:00:01 16 2 0 81
16:00:00 13 2 0 84
17:00:01 22 6 1 71
18:00:00 16 2 1 81
19:00:00 20 4 0 76

I need anything like this:

cat file | awk '{ print $5 }' | awk 'sum all data and / samples(13)'

Thank You in advance...

Re: awk doubt

am 16.01.2008 18:48:33 von Ed Morton

On 1/16/2008 11:42 AM, apogeusistemas@gmail.com wrote:
>
> Hi:
> How could I sum all data from column 5 in this file and divide by
> numbers of samples?
>
> cat file
>
> 7:00:00 4 1 0 95
> 08:00:00 9 2 0 89
> 09:00:00 10 2 0 87
> 10:00:00 15 2 0 83
> 11:00:00 18 2 1 79
> 12:00:00 13 2 0 84
> 13:00:01 4 1 0 94
> 14:00:01 12 2 1 85
> 15:00:01 16 2 0 81
> 16:00:00 13 2 0 84
> 17:00:01 22 6 1 71
> 18:00:00 16 2 1 81
> 19:00:00 20 4 0 76
>
> I need anything like this:
>
> cat file | awk '{ print $5 }' | awk 'sum all data and / samples(13)'
>
> Thank You in advance...

awk '{s+=$5}END{print s/NR}' file

Ed.

Re: awk doubt

am 16.01.2008 18:50:06 von Glenn Jackman

At 2008-01-16 12:42PM, "apogeusistemas@gmail.com" wrote:
> How could I sum all data from column 5 in this file and divide by
> numbers of samples?
>
> cat file
>
> 7:00:00 4 1 0 95
> 08:00:00 9 2 0 89
[...]
>
> cat file | awk '{ print $5 }' | awk 'sum all data and / samples(13)'

awk '{sum += $5} END {printf("%d/%d=%f\n", sum, NR, sum/NR)}' file

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: awk doubt

am 16.01.2008 21:39:18 von someone

apogeusistemas@gmail.com wrote:
>
> Hi:
> How could I sum all data from column 5 in this file and divide by
> numbers of samples?
>
> cat file
>
> 7:00:00 4 1 0 95
> 08:00:00 9 2 0 89
> 09:00:00 10 2 0 87
> 10:00:00 15 2 0 83
> 11:00:00 18 2 1 79
> 12:00:00 13 2 0 84
> 13:00:01 4 1 0 94
> 14:00:01 12 2 1 85
> 15:00:01 16 2 0 81
> 16:00:00 13 2 0 84
> 17:00:01 22 6 1 71
> 18:00:00 16 2 1 81
> 19:00:00 20 4 0 76
>
> I need anything like this:
>
> cat file | awk '{ print $5 }' | awk 'sum all data and / samples(13)'

perl -lane'push @x, /(\d+)$/}{$x += $_ for @x; print $x / @x' file


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: awk doubt

am 17.01.2008 18:05:58 von Glenn Jackman

At 2008-01-16 03:39PM, "John W. Krahn" wrote:
> apogeusistemas@gmail.com wrote:
> > How could I sum all data from column 5 in this file and divide by
> > numbers of samples?
> >
> > cat file
> >
> > 7:00:00 4 1 0 95
> > 08:00:00 9 2 0 89
[...]
>
> perl -lane'push @x, /(\d+)$/}{$x += $_ for @x; print $x / @x' file

If you're going to use -a, use it:

perl -lane'push @x, $F[-1] }{$x += $_ for @x; print $x / @x' file

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: awk doubt

am 17.01.2008 22:44:46 von Martien Verbruggen

On 17 Jan 2008 17:05:58 GMT,
Glenn Jackman wrote:
> At 2008-01-16 03:39PM, "John W. Krahn" wrote:
>> apogeusistemas@gmail.com wrote:
>> > How could I sum all data from column 5 in this file and divide by
>> > numbers of samples?
>> >
>> > cat file
>> >
>> > 7:00:00 4 1 0 95
>> > 08:00:00 9 2 0 89
> [...]
>>
>> perl -lane'push @x, /(\d+)$/}{$x += $_ for @x; print $x / @x' file
>
> If you're going to use -a, use it:
>
> perl -lane'push @x, $F[-1] }{$x += $_ for @x; print $x / @x' file

perl -lane '$s += $F[-1]; END { print $s/$. }' file

Martien
--
|
Martien Verbruggen | Useful Statistic: 75% of the people make up
| 3/4 of the population.
|

Re: awk doubt

am 18.01.2008 15:07:14 von Glenn Jackman

At 2008-01-17 04:44PM, "Martien Verbruggen" wrote:
> On 17 Jan 2008 17:05:58 GMT,
> Glenn Jackman wrote:
> > At 2008-01-16 03:39PM, "John W. Krahn" wrote:
> >> perl -lane'push @x, /(\d+)$/}{$x += $_ for @x; print $x / @x' file
> >
> > If you're going to use -a, use it:
> >
> > perl -lane'push @x, $F[-1] }{$x += $_ for @x; print $x / @x' file
>
> perl -lane '$s += $F[-1]; END { print $s/$. }' file

And we're back to the awk solution Ed and I each provided a couple of
days ago ;)

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Re: awk doubt

am 20.01.2008 00:45:28 von Martien Verbruggen

On 18 Jan 2008 14:07:14 GMT,
Glenn Jackman wrote:
> At 2008-01-17 04:44PM, "Martien Verbruggen" wrote:
>> On 17 Jan 2008 17:05:58 GMT,
>> Glenn Jackman wrote:
>> > At 2008-01-16 03:39PM, "John W. Krahn" wrote:
>> >> perl -lane'push @x, /(\d+)$/}{$x += $_ for @x; print $x / @x' file
>> >
>> > If you're going to use -a, use it:
>> >
>> > perl -lane'push @x, $F[-1] }{$x += $_ for @x; print $x / @x' file
>>
>> perl -lane '$s += $F[-1]; END { print $s/$. }' file
>
> And we're back to the awk solution Ed and I each provided a couple of
> days ago ;)

Which is why I posted it :)

Martien
--
|
Martien Verbruggen | Blessed are the Fundamentalists, for they
| shall inhibit the earth.
|