智能

发布: (2026年1月15日 GMT+8 05:09)
1 min read
原文: Dev.to

Source: Dev.to

用于过滤 CSV 列的 Bash 脚本

#!/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

相关文章

阅读更多 »

Linux教程:日志转CSV到JSON

设置目录 bash mkdir -p tutorial cd tutorial 生成示例日志 bash 在 tutorial 文件夹内生成日志 echo '2026-01-18 05:42:09 | INFO | system...'

计数器

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 '正在转储 ta...'

我的 Bash 脚本编码规则

我的 Bash 脚本编码规范 !封面图片:我的 Bash 脚本编码规范 https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=a...