]>
zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkt-get_median
   4     # Desc: Consumes stdin; outputs as stdout lines 
   5     # Input: stdin (consumes) 
   6     # Output: stdout (newline delimited) 
   7     # Example: printf "foo\nbar\n" | read_stdin 
   8     # Depends: GNU bash (version 5.1.16) 
  10     local input_stdin output
; 
  13     if [[ -p /dev
/stdin 
]]; then 
  14         input_stdin
="$(cat -)"; 
  17     # Store as output array elements 
  19     if [[ -n $input_stdin ]]; then 
  20         while read -r line
; do 
  22         done < <(printf "%s\n" "$input_stdin"); 
  26     printf "%s\n" "${output[@]}"; 
  27 }; # read stdin to stdout lines 
  29     # Desc: Gets the median from integers or floats read from stdin 
  30     # Input:  stdin   newline-delimited integers or floats 
  31     # Output: stdout  float 
  32     # Depends: GNU Coreutils 8.32 (sort), bc 1.07.1 
  33     # Depends: BK-2020-03: read_stdin() 0.0.1, 
  40     list
="$(printf "%s
\n" "$list" | sort -n)"; 
  43     list_lc
="$(printf "%s
\n" "$list" | wc -l)"; 
  45     # Get number of lines to trim from start and end 
  46     if [[ $
((list_lc 
% 2)) -eq 0 ]]; then 
  48         trim
=$
(( list_lc 
/ 2 - 1 )); 
  52         trim
=$
(( list_lc 
/ 2 )); 
  56     list
="$(printf "%s
\n" "$list" | tail -n+$((trim+1)) | head -n-$((trim)) )"; 
  59     if [[ "$flag_even" == "true" ]]; then 
  60         ## Average remaining two lines 
  61         l1
="$(printf "%s
\n" "$list" | head -n1)"; 
  62         l2
="$(printf "%s
\n" "$list" | tail -n1)"; 
  63         median
="$( echo "( $l1 + $l2 ) / 2" | bc -l; )"; 
  65         ## Median is simply remmaining line 
  70     printf "%s" "$median"; 
  72     # Author: Steven Baltakatei Sandoval 
  74 }; # Returns median float from a list 
  77 shuf 
-i1-100 -n49 | get_median
; printf "\n"; 
  78 printf "17\n5\n11.1111\n3.141592\n2\n343.4343434343434343\n" | get_median
; printf "\n";