Need Comma seperated output for N number of values in Shell [duplicate]
-
26-07-2021 - |
Question
Given below is a script with X number of Outputs:
#!/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
Required Output:
cand1, cand2, cand3, cand62
Solution
If you don't require spaces:
... | paste -d, -s -
If you need spaces:
... | paste -d, -s - | sed 's/,/, /g'
OTHER TIPS
Use awk and and change the ORS:
echo $output | awk -v ORS=", " '{print $0}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow