如何将shell脚本的输出格式从一列格式化为多列

时间:2015-06-11 10:29:50

标签: shell unix

我的shell脚本的输出如下(请查看附图)

workflow_Name1
Succeeded
Tue May 19 11:15:33 2015
workflow_Name2
Succeeded
Wed Jun 10 18:00:21 2015

我希望将其更改为

workflow_Name1 :-Succeeded :-Tue May 19 11:15:33 2015
workflow_Name2 :-Succeeded :-Wed Jun 10 18:00:21 2015

以下是我正在使用的脚本。能告诉我如何实现这个目标吗?

#!/bin/bash
# source $HOME/.bash_profile

output=/home/infaprd/cron/output.lst

sqlplus -s user/test@dev <<EOF >$output   # Capture output from SQL
set linesize 55 pages 500
spool output_temp.lst;
set head off;
select sysdate from dual;
set head on;
spool off;
EOF

for name in workflow_Name1 workflow_Name2; do
  pmcmd getworkflowdetails -Repository ${name}
done |
grep -e "Workflow:" -e "Workflow run status:" -e "End time:" | cut -d'[' -f2 | cut -d']' -f1 |

sed -e 's/ *$//' >> $output
mail -s "Output - `date '+%d-%m-%y'`" akhil@gmail.com <$output

1 个答案:

答案 0 :(得分:2)

您可以使用awk

执行此操作
awk '{getline a;getline b; if($0) printf "%-s\n", $0 " :-" a " :-" b}'

输出:

workflow_Name1 :-Succeeded :-Tue May 19 11:15:33 2015
workflow_Name2 :-Succeeded :-Wed Jun 10 18:00:21 2015

您也可以使用sed来完成此任务:

sed 'N;N;s/\n/ :-/g'