DEV Community

Query Filter
Query Filter

Posted on

filter-2

#!/bin/bash
# Usage: ./filter.sh file.csv 3 24 42
# Arguments: first is file name, rest are time columns

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

file="$1"
shift  # remove first arg, now $@ contains only column indexes

# Convert column args to comma-separated string for awk
cols=$(IFS=,; echo "$*")

awk -F'~' -v OFS='~' -v cols="$cols" '
BEGIN {
    # Convert comma-separated string to array of column indexes
    n = split(cols, cidx, ",")
}
{
    for (i=1; i<=n; i++) {
        col = cidx[i]
        if ($col ~ /[0-9]{3}(AM|PM)$/) {
            $col = substr($col, 1, length($col)-6) substr($col, length($col)-1)
        }
    }
    print
}' "$file"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)