
我有一個 Unix shell 腳本,它從日誌產生輸出。
for i in HistSimBasislieferung hedgeAccountingF4PublicSend;do
report1=$(echo -n $i:)
date1=$(grep -e $i $PALME_CONF | grep "The job finished" | tail -1 | awk '{print $1,$2}'|\<br/> sed 's/^.\(.*\).$/\1/')
final="$report1 $date1"
echo "$final"
done
目前輸出如下圖所示:
HistSimBasislieferung 2015-03-11 01:03:02
hedgeAccountingF4PublicSend 2015-03-11 00:16:34
但我想要的是這樣的:
HistSimBasislieferung,hedgeAccountingF4PublicSend
2015-03-11 01:03:02,2015-03-11 00:16:34
您對如何做到這一點有什麼建議嗎?
我照你建議的方式做了:
for i in HistSimBasislieferung hedgeAccountingF4PublicSend;do
echo -n $i > report_log
grep -e $i $PALME_CONF | grep "The job finished" | tail -1 | awk '{print $1,$2}' | sed 's/^.\(.*\).$/\1/' > date_log
done
cat < report_log | xargs echo | tr ' ' ',';
cat < date_log | xargs echo | tr ' ' ',';
echo "" > report_log; echo "" > date_log
但輸出給出:
hedgeAccountingF4PublicSend2015-03-11,00:16:34
I need it like this:
HistSimBasislieferung,hedgeAccountingF4PublicSend,
2015-03-11 03:05:40,2015-03-11 02:12:19,
答案1
這個襯裡可能看起來很難看,但我認為它可以完成工作(如果我正確理解你的用例)。
your_shell_script.sh | cut -d ' ' -f 1 | tr '\n' ',' | sed 's/,$/\n/'; your_shell_script.sh | cut -d ' ' -f '2-' | tr '\n' ',' | sed 's/,$/\n/'
答案2
它是一個循環腳本,因此每次循環都會覆蓋變數。我的建議是為“報告”建立日誌文件,並為“日期”建立日誌文件(附加日誌)。
在腳本末尾(“done”行正下方),執行以下命令:
cat <\report_log_file> | xargs echo | tr ' ' ',';
cat <\date_log_file> | xargs echo | | tr ' ' ','
或建立另一個循環腳本。
for i in <\report_log_file> <\date_log_file>; do
cat $i | xargs echo | tr ' ' ',';
done
然後,清除日誌檔:
echo "" > report_log_file; echo "" > date_log_file