I'm having some problems with a script parsing a .csv file
Information on .csv:
#1,13/8/2020,somedude@hotmail.com,otherdude@hotmail.com,,Subject,"Dear Dude,
Information have been updated. Please login to APP to review by 30th August 2020. Thank you.
Best regards,
Mr. Mack",Pending,13/8/2020 12:35
This is how information should be partioned: MsgID,Date,To,CC,BCC,Subject,Body,Status,Timestamp
MsgID=#1
Date=13/8/2020
To=somedude@hotmail.com
CC=otherdude@hotmail.com
BCC=
Subject=Subject
Body=Dear Dude,
Information have been updated. Please login to APP to review by 30th August 2020. Thank you.
Best regards,
Mr. Mack
Status=Pending
Timestamp=13/8/2020 12:35
The problems I'm having are with the body part for two reasons, the "," in the text that breaks when I'm trying to run the script and the other reason is the line breaks in the text.
This is the script that I was doing:
#!/bin/bash
export TIMESTAMP="$( date '+%d/%m/%Y %H:%M:%S' )"
INPUT=/tmp/test.csv
OUTPUT=/tmp/test.csv.out
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read msgid dat to cc bcc subject body status timesta
do
echo "MSG ID : $msgid"
echo "Date : $dat"
echo "To : $to"
echo "CC : $cc"
echo "BCC : $bcc"
echo "Subject : $subject"
echo "Body : $body"
echo "Status : $status"
echo "Timestamp : $timesta"
echo $body | mail -s "$subject" $to -c $cc -b $bcc
printf "$msgid,$dat,$to,$cc,$bcc,$subject,$body,Sent,$TIMESTAMP" >> $OUTPUT
done < $INPUT
IFS=$OLDIFS
printf %q
to escape strings in POSIX shell format? So the body will be single line with \n escapes and anything that could cause issue would be escaped?printf %q "$body"
– Jakub Jindra Aug 14 '20 at 09:58;
, shouldn't that be,
? – terdon Aug 14 '20 at 10:15,
csv for email, really?Best regards,
– alecxs Aug 14 '20 at 11:05