Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. Id ht
  2. 510 69
  3. 510 67
  4. 510 65
  5. 510 62
  6. 510 59
  7. 601 29
  8. 601 26
  9. 601 21
  10. 601 20
  11.  
  12. Id Avg.ht
  13. 510 64
  14. 601 24
  15.  
  16. $ cat FILE
  17. Id ht
  18. 510 69
  19. 510 67
  20. 510 65
  21. 510 62
  22. 510 59
  23. 601 29
  24. 601 26
  25. 601 21
  26. 601 20
  27.  
  28. $ awk '
  29. NR>1{
  30. arr[$1] += $2
  31. count[$1] += 1
  32. }
  33. END{
  34. for (a in arr) {
  35. print "id avg " a " = " arr[a] / count[a]
  36. }
  37. }
  38. ' FILE
  39.  
  40. $ perl -lane '
  41. END {
  42. foreach my $key (keys(%hash)) {
  43. print "id avg $key = " . $hash{$key} / $count{$key};
  44. }
  45. }
  46. if ($. > 1) {
  47. $hash{$F[0]} += $F[1];
  48. $count{$F[0]} += 1;
  49. }
  50. ' FILE
  51.  
  52. id avg 601 = 24
  53. id avg 510 = 64.4
  54.  
  55. perl -lane'END{for(keys(%h)){print"$_:".$h{$_}/$c{$_}}}($.>1)&&do{$h{$F[0]}+=$F[1];$c{$F[0]}++}' FILE
  56.  
  57. #!/usr/bin/perl
  58. use strict;
  59. use warnings;
  60.  
  61. my %sum_so_far;
  62. my %count_so_far;
  63. while ( <> ) {
  64. # Skip lines that don't start with a digit
  65. next if m/^[^d]/;
  66.  
  67. # Accumulate the sum and the count
  68. my @line = split();
  69. $sum_so_far{$line[0]} += $line[1];
  70. $count_so_far{$line[0]} += 1;
  71. }
  72.  
  73. # Dump the output
  74. print "Id Avg.htn";
  75. foreach my $id ( keys %count_so_far ) {
  76. my $avg = $sum_so_far{$id}/$count_so_far{$id};
  77. print " $id $avgn";
  78. }
  79.  
  80. ire@localhost$ perl make_average.pl input.txt
  81. Id Avg.ht
  82. 510 64.4
  83. 601 24
  84.  
  85. datamash -H -s -g 1 mean 2 <file
  86.  
  87. s
Add Comment
Please, Sign In to add comment