Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. cat filename.txt | grep x_y | (this need to be filled)
  2.  
  3. perl -pi -e 's/x_y/m_n/g'
  4.  
  5. perl -nE '$s+=$1, ++$n if /x_y=(d+)/; END { say "avg:", $s/$n }' data.txt
  6.  
  7. ... | grep 'x_y=' | perl -ne '$x += (split /=/, $_)[1]; $y++ }{ print $x/$y, "n"'
  8.  
  9. /^[^_]+_[^=]+=[0-9]+$/ {sum=sum+$2; cnt++}
  10. END {
  11. print "sum:", sum, "items:", cnt, "avg:", sum/cnt
  12. }
  13.  
  14. $ awk -F= -f cnt.awk data.txt
  15. sum: 55 items: 10 avg: 5.5
  16.  
  17. #!/bin/bash
  18.  
  19. while IFS='=' read str num
  20. do
  21. if [[ $str == *_* ]]
  22. then
  23. sum=$((sum + num))
  24. cnt=$((cnt + 1))
  25. fi
  26.  
  27. done < data.txt
  28.  
  29. echo "scale=4; $sum/$cnt" | bc ;exit
  30.  
  31. $ ./cnt.sh
  32. 5.5000
  33.  
  34. perl -nlwe '
  35. push @a, /x_y=(d+)/g # push all matches onto an array
  36. }{ # eskimo-operator, is evaluated last
  37. $sum += $_ for @a; # get the sum
  38. print "Average: ", $sum / @a; # divide by the size of the array
  39. ' input.txt
  40.  
  41. perl -nlwe 'push @a, /x_y=(d+)/g }{ $sum += $_ for @a; print "Average: ", $sum / @a;' input.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement