10

I created a script that was using

cut -d',' -f- --output-delimiter=$'\n'

to add a newline for each command separated value in RHEL 5, for e.g.

[root]# var="hi,hello how,are you,doing"
[root]# echo $var
hi,hello how,are you,doing
[root]# echo $var|cut -d',' -f- --output-delimiter=$'\n'
hi
hello how
are you
doing

But unfortunately when I run the same command in Solaris 10, it doesn't work at all :( !

bash-3.00# var="hi,hello how,are you,doing"
bash-3.00# echo $var
hi,hello how,are you,doing
bash-3.00# echo $var|cut -d',' -f- --output-delimiter=$'\n'
cut: illegal option -- output-delimiter=

usage: cut -b list [-n] [filename ...]
       cut -c list [filename ...]
       cut -f list [-d delim] [-s] [filename]

I checked the man page for 'cut' and alas there is no ' --output-delimiter ' in there !

So how do I achieve this in Solaris 10 (bash)? I guess awk would be a solution, but I'm unable to frame up the options properly.

Note: The comma separated variables might have " " space in them.

Marcos
  • 845
  • 3
  • 10
  • 21
  • If you want the same cut command you have on RHEL, install the [GNU coreutils](https://www.gnu.org/software/coreutils/) package. – alanc Nov 28 '13 at 06:23

2 Answers2

9

What about using tr for this?

$ tr ',' '\n' <<< "$var"
hi
hello how
are you
doing

or

$ echo $var | tr ',' '\n'
hi
hello how
are you
doing

With :

$ sed 's/,/\n/g' <<< "$var"
hi
hello how
are you
doing

Or with :

$ awk '1' RS=, <<< "$var"
hi
hello how
are you
doing
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    Somehow I'm feeling like a BIG DUMBO!!! I never thought of using 'tr' or 'sed'...pfffttt..... Thanks a lot for the answer! – Marcos Nov 25 '13 at 14:43
  • 'sed' didn't work, bash-3.00# echo $var|sed -e 's/,/\n/g' hinhello hownare youndoing but 'tr' worked! – Marcos Nov 25 '13 at 14:50
  • Uhms I don't have a Solaris server to test, but maybe in http://stackoverflow.com/questions/8991275/escaping-newlines-in-sed-replacement-string you can find some clues. And good to read `tr` was fine for you :) – fedorqui Nov 25 '13 at 14:53
3

Perhaps do it in itself?

var="hi,hello how,are you,doing"
printf "$var" | (IFS=, read -r -a arr; printf "%s\n" "${arr[@]}")
hi
hello how
are you
doing
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • thanks for the answer, but I would prefer something smaller also I would like not to use an array for this. – Marcos Nov 25 '13 at 14:52