2015-01-21 73 views
0

我正在使用ksh。從字符分隔文件中讀取並分配給變量| ksh Unix shell

我需要將文件中的數據讀入變量,然後進一步使用它們發送電子郵件。

  • 文件可以由任何較少使用的字符(如| ^等)或一組字符分隔。
  • 需要檢索從郵件,郵件到,抄送,密送,主題,身體從文件。
  • 我從表中假脫機到文件,因此分隔符可以是任何字符,但較少用於一般的英語,因爲字符,如& *等可能存在於身體,並可能返回錯誤的值。

文件:CCBCC沒有在文件中提供即它們是空白)使用

[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] 
+0

你不只是需要解析第一線? – SMA 2015-01-21 08:28:03

+0

它不只是一條線。相反,它只是一個單一的條目。問題是,最後一個條目是電子郵件的BODY,它本身確實有很多行。 – Sachin 2015-01-21 08:30:10

+0

也許可以使用'cut'來分配每個變量? 'cut -f1 -d'|''使用f1到f6? – Jmoreland91 2015-01-21 16:46:24

回答

0

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

有時我喜歡的地方使用sedhead/tail

+0

唯一的問題是僅在BODY中,這裏又來了。所有其他字段都顯示正常,但是對於正文,它顯示了這個錯誤:generate_error_file.ksh [174]:body + = $ \ n:not found .. PS:我是shell的新手,因此沒有那麼多它的知識。 – Sachin 2015-01-21 12:18:51

+0

另外,我發現了一些替代解決方案,將BODY在另一個文件中單獨打印並使用。無論如何,我將直接在'mailx'命令中使用該文件作爲主體。 – Sachin 2015-01-21 12:21:08

+0

你可能有一個較老的ksh。我更新了文本附加到body變量的方式 – 2015-01-21 12:24:00

0

我用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] 
相關問題