从字符分隔文件中读取并分配给变量| ksh Unix shell
我正在使用ksh。从字符分隔文件中读取并分配给变量| ksh Unix shell
我需要将文件中的数据读入变量,然后进一步使用它们发送电子邮件。
- 文件可以由任何较少使用的字符(如| ^等)或一组字符分隔。
- 需要检索从邮件,邮件到,抄送,密送,主题,身体从文件。
- 我从表中假脱机到文件,因此分隔符可以是任何字符,但较少用于一般的英语,因为字符,如& *等可能存在于身体,并可能返回错误的值。
文件:(CC和BCC没有在文件中提供即它们是空白)使用
[email protected]|[email protected]|||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:
[email protected]
代码:
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
它没有设置变量。
及以下使用代码时:它示出了不期望的输出:
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
输出:(地狱很多空行)
[email protected]
[email protected]
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:
[email protected]
在KSH,最后一个命令在一个管道中在当前shell中执行。
我会处理第一行,然后将所有其他行添加到body
变量。
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: [email protected]
to: [email protected]
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:
[email protected]
有时我喜欢的地方使用sed
的head/tail
唯一的问题是仅在BODY中,这里又来了。所有其他字段都显示正常,但是对于正文,它显示了这个错误:generate_error_file.ksh [174]:body + = $ \ n:not found .. PS:我是shell的新手,因此没有那么多它的知识。 – Sachin 2015-01-21 12:18:51
另外,我发现了一些替代解决方案,将BODY在另一个文件中单独打印并使用。无论如何,我将直接在'mailx'命令中使用该文件作为主体。 – Sachin 2015-01-21 12:21:08
你可能有一个较老的ksh。我更新了文本附加到body变量的方式 – 2015-01-21 12:24:00
我用cut
命令,它似乎运作良好将其分配给变量。虽然,我不确定您的文件是否有多个条目。
Testing.sh
#!/usr/bin/ksh
a1=$(cat test.txt | cut -f1 -d '|' -s)
a2=$(cat test.txt | cut -f2 -d '|' -s)
a3=$(cat test.txt | cut -f3 -d '|' -s)
a4=$(cat test.txt | cut -f4 -d '|' -s)
a5=$(cat test.txt | cut -f5 -d '|' -s)
a6=$(cat test.txt | cut -f6 -d '|')
echo $a1
echo $a2
echo $a3
echo $a4
echo $a5
echo $a6
测试。TXT
[email protected]|[email protected]|||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:
[email protected]
输出:
$Testing.sh
[email protected]
[email protected]
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: [email protected]
你不只是需要解析第一线? – SMA 2015-01-21 08:28:03
它不只是一条线。相反,它只是一个单一的条目。问题是,最后一个条目是电子邮件的BODY,它本身确实有很多行。 – Sachin 2015-01-21 08:30:10
也许可以使用'cut'来分配每个变量? 'cut -f1 -d'|''使用f1到f6? – Jmoreland91 2015-01-21 16:46:24