Reputation: 1158
I know how to pass variables to awk, but I haven't been able to combine that with the column number functionality when working with a CSV. I'm also not sure how to perform a string concatenation this way.
I'm expecting some columns in my CSVs to contain dates in MM-dd-yyyy
or yyyy/MM/dd
format, or some permutation of those, and would like to append a default hh:mm:ss
onto it, so a column of 1/10/2017
becomes 1/10/2017 00:00:00
. I've tried doing something along these lines in awk, but it doesn't seem to recognize the column I'm trying to reference. And I don't think this is how string concatenation is done.
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
Upvotes: 0
Views: 245
Reputation: 1463
It would be easier to answer your question if you provided a minimal reproducible example of your input data.
Without knowing the data, I assume that you should get what you're looking by replacing your awk command with the following:
awk -v col=$(($i + 1)) 'BEGIN{FS=", "} {print $col " 00:00:00"}' "$file"
Upvotes: 1