#!/usr/bin/env bash
# Usage:
# zero_small.sh [-t threshold] [-z zero_value] [file]
#
# Examples:
# zero_small.sh data.csv
# cat data.csv | zero_small.sh
# zero_small.sh -t 1e-30 -z 0 data.csv
THRESHOLD="1e-20"
ZERO_VAL="0.0"
while getopts "t:z:" opt; do
case "$opt" in
t) THRESHOLD="$OPTARG" ;;
z) ZERO_VAL="$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
awk -F'~' -v OFS='~' -v thr="$THRESHOLD" -v zero="$ZERO_VAL" '
{
for (i = 1; i <= NF; i++) {
# numeric (incl scientific)
if ($i ~ /^-?[0-9.]+([eE][-+]?[0-9]+)?$/) {
val = $i + 0
if (val != 0 && val < thr && val > -thr) {
$i = zero
}
}
}
print
}
' "${1:-/dev/stdin}"
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)