-
-
Save flashvoid/d685228267e853c278a93fc7f59d2d84 to your computer and use it in GitHub Desktop.
Take file with column of numbers, in every block of 5 filter out ones larger then token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
12 | |
31 | |
4 | |
5 | |
6 | |
3 | |
76 | |
34 | |
87 | |
57 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: ./thisscript /path/to/file <int> | |
die () { | |
echo $1 | |
exit $2 | |
} | |
test -f $1 || die "No such file $1" 1 | |
[[ $2 ]] || die "Expect number as a second parameter" | |
# filter_min takes line with numbers on stdin and minimal value as first parameter | |
# e.g. echo "34 87 57 1 2" | filter_min 5 | |
# output 34 87 57 | |
filter_min() { xargs -n1 | while read line; do [[ $line -gt $1 ]] && echo $line; done; } | |
# count_min takes line with numbers on stdin and counts number of numbers larger then min | |
count_min () { xargs -n1 | awk -v min=$1 '{ if ($1 > min) {sum+=1} } END { print sum }'; } | |
# outputs 0 if stdin is empty, otherwise outputs stdin | |
zero_if_empty () { a=$(cat -); [[ $a -gt 0 ]] && echo $a || echo 0; } | |
c=0 | |
for line in $(cat $1); do | |
[[ $c -gt 9 ]] && c=0; # if c > 4 then reset c to 0 | |
[[ $c == 0 ]] && arr=$line || arr="$arr $line"; # if c == 0 initialize new array, else append value to array | |
c=$(($c + 1)); # increment c | |
[[ $c == 10 ]] && echo $arr | count_min $2 | zero_if_empty; # if c == 10 filter array through function | |
done | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 | |
7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment