I have a list like below : release=abc,xyz,pqr,bnm and as output I need
abc
xyz
pqr
bnm
I have tried below using sed , it successfully removes , but not able to insert new line after comma , if can help pls
release=abc,xyz,pqr,bnm
for i in $release;do
b=`echo $i| sed 's/,/\\n/g'`
echo $b
done
output: abc xyz pqr bnm which is not expected here as need new line
CodePudding user response:
Use Parameter Expansion.
release=abc,xyz,pqr,bnm
echo "${release//,/$'\n'}"
Output
abc
xyz
pqr
bnm
With your approach using sed
release=abc,xyz,pqr,bnm
echo "$release" | sed 's/,/\n/g'
CodePudding user response:
There is no need for a loop, as the i
in the loop is abc,xyz,pqr,bnm
You could also just translate the newlines to commas:
release=abc,xyz,pqr,bnm
tr , '\n' <<< $release
Output
abc
xyz
pqr
bnm
If there should be a non whitespace char other than a comma before actually match the comma, you can use sed
with a capture group:
release=abc,xyz,pqr,bnm
sed 's/\([^[:space:],]\),/\1\n/g' <<< $release