【问题标题】:Need Comma seperated output for N number of values in Shell [duplicate]Shell中N个值需要逗号分隔输出[重复]
【发布时间】:2012-10-22 09:38:33
【问题描述】:

下面是一个有 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

【问题讨论】:

  • 不清楚你想做什么。你有什么问题?
  • 当我尝试运行上述脚本时,我得到如下输出:cand1 cand2 cand3 cand62。但我需要以逗号分隔的输出为 cand1、cand2、cand3、cand62
  • 外面有人吗???请帮助 A.S.A.P
  • 你会解决这个问题吗?这至少是与同一查询相关的第三个问题。祝你好运。
  • 你确定echo $output 产生多行输出吗?

标签: shell unix scripting scripting-language


【解决方案1】:

如果您不需要空格:

... | paste -d, -s -

如果你需要空格:

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

【讨论】:

  • 它对我没有帮助....它提供相同的输出......echo $output |粘贴 -d, -s -
  • 伙计们,下面的查询对我有用......我使用了 listagg 函数......在组内选择 listagg(o.candidateid, ',') (按加入日期排序,o.candidateid)来自onboardingcandidates o, Candidatedetails c where o.candidateid=c.candidateid and o.JOININGSTATUS='0091' and to_date(o.joiningdate)=to_date(sysdate+5);
【解决方案2】:

使用 awk 并更改 ORS:

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

【讨论】:

  • 这可以简化:awk 1 ORS=", "
  • 伙计们,下面的查询对我有用......我使用了 listagg 函数.....在组内选择 listagg(o.candidateid, ',') (按加入日期排序,o.candidateid)来自onboardingcandidates o, Candidatedetails c where o.candidateid=c.candidateid and o.JOININGSTATUS='0091' and to_date(o.joiningdate)=to_date(sysdate+5);
猜你喜欢
  • 2021-06-23
  • 1970-01-01
  • 2017-07-04
  • 2019-12-25
  • 2013-10-16
  • 2011-07-26
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多