0

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
1
  • Sorry to break this to you, but you actually don't know how to pass shell variables to awk: using single quotes will prevent the variable substitution. Commented Oct 17, 2017 at 0:08

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.