Search code examples
unixkshdelimiterreadfileifs

Read from character delimted file and assign into variables | ksh Unix shell


I am using ksh.

I need to read the data from file, into the variables and then further use them for sending emails.

  • File can be delimited by any less used characters (like | ^ etc.) or group of characters.
  • Need to retrieve mail from, mail to,cc,bcc, subject, body from file.
  • I am spooling into file from table, thus delimiter can be any character, but less used in general english because characters like , & * etc. might be present in Body and that may return wrong value.

File: (cc and bcc are not available in the file i.e they are blank)

na734@company.com|NA734@company.COM|||TEST EMAIL FOR LMS ERROR|Hi <<FIRST_NAME>>, <br><br>

Following errors are generated during migration of LMS data into FIMS application.<br><br><br>
The respective details of Error(s) occured is logged into the attached file.
Regards,<br>
FIMS Web Application<br><br><br>

This is an auto-generated e-mail, please don't reply to this e-mail
Reply to the following person for further details:
na734@company.com

Code using:

while IFS='|' read -r a1 a2 a3 a4 a5 a6
do 
flag1=`echo $a1`
flag2=`echo $a2`
flag3=`echo $a3`
flag4=`echo $a4`
flag5=`echo $a5`
flag6=`echo $a6`
done < $RUNTIME/EMAIL_$System`date +%y%m%d`.csv

It is not setting the variables.

And when using code below: It is showing undesired output:

while IFS='|' read -r a1 a2 a3 a4 a5 a6
do 
echo $a1
echo $a2
echo $a3
echo $a4
echo $a5
echo $a6
done < $RUNTIME/EMAIL_$System`date +%y%m%d`.csv

Output: (Hell lot of blank lines)

na734@company.com
NA734@company.COM


TEST EMAIL FOR LMS ERROR
Hi <<FIRST_NAME>>, <br><br>






Following errors are generated during migration of LMS data into FIMS application.<br><br><br>





The respective details of Error(s) occured is logged into the attached file.





Regards,<br>





FIMS Web Application<br><br><br>











This is an auto-generated e-mail, please don't reply to this e-mail





Reply to the following person for further details:





na734@company.com

Solution

  • In ksh, the last command in a pipeline is executed in the current shell.

    I would process the first line, then append all the other lines to the body variable.

    file="$RUNTIME/EMAIL_$System$(date +%y%m%d).csv"
    sed 1q "$file" | IFS="|" read -r from to cc bcc subj body
    body="$body
    $(sed 1d "$file")"
    
    printf "%s: %s\n" from "$from" to "$to" cc "$cc" bcc "$bcc" subj "$subj" body "$body"
    
    from: na734@company.com
    to: NA734@company.COM
    cc:
    bcc:
    subj: TEST EMAIL FOR LMS ERROR
    body: Hi <<FIRST_NAME>>, <br><br>
    
    Following errors are generated during migration of LMS data into FIMS application.<br><br><br>
    The respective details of Error(s) occured is logged into the attached file.
    Regards,<br>
    FIMS Web Application<br><br><br>
    
    This is an auto-generated e-mail, please don't reply to this e-mail
    Reply to the following person for further details:
    na734@company.com
    

    I sometimes like to use sed in place of head/tail