I have a csv file (named a.csv
) exported from DB mongo like this:
...
ABC,DN,1000,RENEW_DAY,Thu Apr 27 2017 23:19:47 GMT+0700 (ICT),1,1,4,-1
ANSLAS,DN,1000,RENEW_DAY,Thu Apr 27 2017 23:19:52 GMT+0700 (ICT),1,1,4,-1
...
I want to import this file to MySQL but this date format cannot be inserted. So I have an idea to convert this to be b.csv
before insert to MySQL
...
ABC,DN,1000,RENEW_DAY,2017-04-27 23:19:47,1,1,4,-1
ANSLAS,DN,1000,RENEW_DAY,2017-04-27 23:19:52,1,1,4,-1
...
This is the bash command bash I use to convert date in bash date -d"Thu Apr 27 2017 23:19:52 GMT+0700 (ICT)" +'%Y-%m-%d %H:%M:%S'
So how can I convert this automatically with the bash shell like this ./process.sh a.csv b.csv
?
process.sh
#!/bin/bash
# Environment
filename=$1
filelines=`cat $filename`
for phone in $filelines ;
do
<HOW CAN CUT Filed Date and convert this and export to b.csv file>
done
*
or something in the variables. But since we don't want splitting or globbing, quoting is a good practice. – ilkkachu May 31 '17 at 07:40echo
, but I didn't think of globbing. Thank you. I tried to generate an example that leads to failure, but didn't succeed. – Philippos May 31 '17 at 07:53IFS
, it would change the separator to spaces. tryIFS=,; read a b <<< 1,2,3 ; echo $a,$b; echo "$a,$b"
. Though in this context you could just changeIFS
only for theread
, withIFS=, read ...
(without the semicolon in between) – ilkkachu May 31 '17 at 08:19https://unix.stackexchange.com/questions/344794/bash-converting-date-in-a-csv-file-with-awk-or-other-linux-tool-csvcut
in my case. – Uncelvel Jun 01 '17 at 01:38