Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cat filename.txt | grep x_y | (this need to be filled)
- perl -pi -e 's/x_y/m_n/g'
- perl -nE '$s+=$1, ++$n if /x_y=(d+)/; END { say "avg:", $s/$n }' data.txt
- ... | grep 'x_y=' | perl -ne '$x += (split /=/, $_)[1]; $y++ }{ print $x/$y, "n"'
- /^[^_]+_[^=]+=[0-9]+$/ {sum=sum+$2; cnt++}
- END {
- print "sum:", sum, "items:", cnt, "avg:", sum/cnt
- }
- $ awk -F= -f cnt.awk data.txt
- sum: 55 items: 10 avg: 5.5
- #!/bin/bash
- while IFS='=' read str num
- do
- if [[ $str == *_* ]]
- then
- sum=$((sum + num))
- cnt=$((cnt + 1))
- fi
- done < data.txt
- echo "scale=4; $sum/$cnt" | bc ;exit
- $ ./cnt.sh
- 5.5000
- perl -nlwe '
- push @a, /x_y=(d+)/g # push all matches onto an array
- }{ # eskimo-operator, is evaluated last
- $sum += $_ for @a; # get the sum
- print "Average: ", $sum / @a; # divide by the size of the array
- ' input.txt
- 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