Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Id ht
- 510 69
- 510 67
- 510 65
- 510 62
- 510 59
- 601 29
- 601 26
- 601 21
- 601 20
- Id Avg.ht
- 510 64
- 601 24
- $ cat FILE
- Id ht
- 510 69
- 510 67
- 510 65
- 510 62
- 510 59
- 601 29
- 601 26
- 601 21
- 601 20
- $ awk '
- NR>1{
- arr[$1] += $2
- count[$1] += 1
- }
- END{
- for (a in arr) {
- print "id avg " a " = " arr[a] / count[a]
- }
- }
- ' FILE
- $ perl -lane '
- END {
- foreach my $key (keys(%hash)) {
- print "id avg $key = " . $hash{$key} / $count{$key};
- }
- }
- if ($. > 1) {
- $hash{$F[0]} += $F[1];
- $count{$F[0]} += 1;
- }
- ' FILE
- id avg 601 = 24
- id avg 510 = 64.4
- perl -lane'END{for(keys(%h)){print"$_:".$h{$_}/$c{$_}}}($.>1)&&do{$h{$F[0]}+=$F[1];$c{$F[0]}++}' FILE
- #!/usr/bin/perl
- use strict;
- use warnings;
- my %sum_so_far;
- my %count_so_far;
- while ( <> ) {
- # Skip lines that don't start with a digit
- next if m/^[^d]/;
- # Accumulate the sum and the count
- my @line = split();
- $sum_so_far{$line[0]} += $line[1];
- $count_so_far{$line[0]} += 1;
- }
- # Dump the output
- print "Id Avg.htn";
- foreach my $id ( keys %count_so_far ) {
- my $avg = $sum_so_far{$id}/$count_so_far{$id};
- print " $id $avgn";
- }
- ire@localhost$ perl make_average.pl input.txt
- Id Avg.ht
- 510 64.4
- 601 24
- datamash -H -s -g 1 mean 2 <file
- s
Add Comment
Please, Sign In to add comment