#!/bin/bash
# Usage:
# ./roundFilter.sh file.csv decimals col1 [col2 ... colN]
#
# Example:
# ./roundFilter.sh data.csv 2 45 12 78
if [ "$#" -lt 3 ]; then
echo "Usage: $0 file.csv decimals col1 [col2 ... colN]"
exit 1
fi
file="$1"
decimals="$2"
shift 2 # now $@ contains only column indexes
# Convert column list to comma-separated string
cols=$(IFS=,; echo "$*")
awk -F'~' -v OFS='~' -v cols="$cols" -v dec="$decimals" '
BEGIN {
n = split(cols, cidx, ",")
fmt = "%." dec "f"
}
{
for (i = 1; i <= n; i++) {
col = cidx[i]
# Only touch non-empty numeric fields
if ($col ~ /^-?[0-9]+(\.[0-9]+)?$/) {
$col = sprintf(fmt, $col)
}
}
print
}
' "$file"
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)