【发布时间】:2017-10-16 20:59:42
【问题描述】:
我知道如何将变量传递给 awk,但是在使用 CSV 时我无法将其与列号功能结合起来。我也不确定如何以这种方式执行字符串连接。
我希望我的 CSV 中的某些列包含 MM-dd-yyyy 或 yyyy/MM/dd 格式的日期,或者它们的某种排列,并希望在其上附加一个默认的 hh:mm:ss,所以一列 @987654324 @ 变为 1/10/2017 00:00:00。我已经尝试在 awk 中按照这些思路做一些事情,但它似乎无法识别我试图引用的列。而且我认为这不是字符串连接的完成方式。
for file in *.csv; do
headline=$(head -n 1 $file)
byline=$(sed '2q;d' $file)
IFS=', ' read -r -a headers <<< $headline
IFS=', ' read -r -a entries <<< $byline
for i in "${!headers[@]}"; do
date='^[0-9]+[/-][0-9]+[/-][0-9]+$'
if [[ ${entries[$i]} =~ $date ]] ; then
awk -F, -v col='$i' '{$($col)+="00:00:00";}1' OFS=, $file
fi
done
done
【问题讨论】:
-
很抱歉打断你,但你实际上不知道如何将 shell 变量传递给 awk:使用单引号会阻止变量替换。