在Shell中需要逗号分隔输出N个值

时间:2012-11-01 18:19:52

标签: shell unix scripting scripting-language

下面给出了一个X行输出的脚本:

#!/bin/bash

instant_client="/root/ora_client/instantclient_11_2"
output=`$instant_client/sqlplus -s HRUSER/HRUSER@TOMLWF <<EOF
set heading off
set feedback off
set lines 10000
set pagesize 10000
select count (1) from onboardingcandidates o, candidatedetails c where o.candidateid=c.candidateid and     o.JOININGSTATUS='0091' and to_date(o.joiningdate)=to_date(sysdate+5);

EOF

exit`

echo $output

Output:
cand1
cand2
cand3
cand62

必需输出:

cand1, cand2, cand3, cand62

2 个答案:

答案 0 :(得分:1)

如果您不需要空格:

... | paste -d, -s -

如果您需要空格:

... | paste -d, -s - | sed 's/,/, /g'

答案 1 :(得分:0)

使用awk并更改ORS:

echo $output | awk -v ORS=", "  '{print $0}'