【问题标题】:Read from character delimted file and assign into variables | ksh Unix shell从字符分隔文件中读取并赋值给变量 | ksh Unix 外壳
【发布时间】:2015-01-21 08:14:06
【问题描述】:

我正在使用 ksh。

我需要将文件中的数据读取到变量中,然后进一步将它们用于发送电子邮件。

  • 文件可以由任何较少使用的字符(如 | ^ 等)或字符组分隔。
  • 需要从文件中检索ma​​il from, mail to,cc,bcc, subject, body
  • 我正在从表格假脱机到文件中,因此分隔符可以是任何字符,但在一般英语中较少使用,因为像 , & * 等字符可能存在于 Body 中并且可能返回错误值。

文件:ccbcc 在文件中不可用,即它们为空白)

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

代码使用:

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

输出:大量空行

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

【问题讨论】:

  • 不用解析第一行吗?
  • 不只是一行。相反,它是一个单一的条目。问题是,最后一个条目是电子邮件的正文,它本身确实有很多行。
  • 可以使用cut 来分配每个变量吗? cut -f1 -d '|' 使用 f1 到 f6?

标签: unix ksh delimiter readfile ifs


【解决方案1】:

在 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: 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

我有时喜欢用sed 代替head/tail

【讨论】:

  • 唯一的问题只出在 BODY 上,又来了。所有其他字段都显示正常,但对于正文,它显示此错误generate_error_file.ksh[174]: body+=$\n: not found .. PS:我是 shell 新手,因此对它没有太多了解。
  • 另外,我发现了一些替代解决方案,将 BODY 单独放在另一个文件中,然后使用它。在任何情况下,我都会将该文件直接用作mailx 命令中的正文。
  • 您的 ksh 可能较旧。我已经更新了文本附加到正文变量的方式
【解决方案2】:

我使用了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

test.txt

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

输出:

$Testing.sh
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2011-08-08
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多