smart

Published: (January 14, 2026 at 04:09 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Bash script for filtering CSV columns

#!/bin/bash
# Usage:
#   ./filter.sh file.csv decimals col1 [col2 ... colN]
#
# Example:
#   ./filter.sh data.csv 2 45

if [ "$#" -lt 3 ]; then
    echo "Usage: $0 file.csv decimals col1 [col2 ... colN]"
    exit 1
fi

file="$1"
decimals="$2"
shift 2

cols=$(IFS=,; echo "$*")

awk -F'~' -v OFS='~' -v cols="$cols" -v dec="$decimals" '
BEGIN {
    n = split(cols, cidx, ",")

    # scaling factor: 10^dec
    scale = 1
    for (i = 1; i = 0 ? 0.5 : -0.5)) / scale

            $col = sprintf(fmt, rounded)
        }
    }
    print
}
' "$file"
Back to Blog

Related posts

Read more »

Linux Tutorial: Logs to CSV to JSON

Setup Directory bash mkdir -p tutorial cd tutorial Generate Sample Logs bash Generate logs inside the tutorial folder echo '2026-01-18 05:42:09 | INFO | system...

A Linux Tutorial: Log to CSV to JSON

Overview This tutorial walks through the process of converting raw application logs into structured JSON data. The workflow is useful for generating test data...

counter

bash mkfifo counter.fifo t=0 b=0 while read cmd; do case '$cmd' in inc_t t++ ;; inc_b b++ ;; get echo '$t $b' ;; esac done counter.fifo echo '' echo 'Dumping ta...

Sort by File Extension - Bash Script

Overview This Bash script organizes the contents of a directory by file extension. It creates a sorted_files folder inside the source directory and, for each d...