| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #!/bin/bash
- # 参数检查
- if [ $# -ne 3 ]; then
- echo "用法: $0 文件 列名或列号 比较值"
- exit 1
- fi
- file="$1"
- column="$2"
- value="$3"
- # 文件存在性检查
- if [ ! -f "$file" ]; then
- echo "错误: 文件 '$file' 不存在"
- exit 1
- fi
- # 列号解析逻辑
- if [[ "$column" =~ ^[0-9]+$ ]]; then
- col_num=$column
- else
- col_num=$(awk -F, -v col="$column" 'NR==1 {
- for(i=1; i<=NF; i++) {
- if(tolower($i) == tolower(col)) {
- print i
- exit
- }
- }
- }' "$file")
- if [ -z "$col_num" ]; then
- echo "错误: 列名 '$column' 未找到"
- exit 1
- fi
- fi
- # 执行筛选的AWK命令
- awk -F, -v col="$col_num" -v val="$value" '
- BEGIN { OFS=FS }
- NR == 1 {
- print
- next
- }
- col < 1 || col > NF {
- print "错误: 无效列号 "col | "cat >&2"
- exit 1
- }
- $col+0 > val+0
- ' "$file"
|