Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacement for cut --output-delimiter

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.

like image 896
Marcos Avatar asked Nov 25 '13 14:11

Marcos


People also ask

What is the default delimiter for cut?

The tab character is the default delimiter of cut, so by default, it considers a field to be anything delimited by a tab. Remember that the "space" between each word is actually a single tab character, so both lines of output are displaying ten characters: eight alphanumeric characters and two tab characters.

What is output delimiter?

By specifying the --output-delimiter option, you can change the separator that is output together with the selected fields. list. Specifies either column positions or field positions as separated by the delimiter. Column positions start from 1.

Which option for the cut command is used to specify a delimiter?

List of the fields number specified must be separated by comma. Ranges are not described with -f option. cut uses tab as a default field delimiter but can also work with other delimiter by using -d option.

How do I change the delimiter in Linux?

Shell script to change the delimiter of a file:Using the shell substitution command, all the commas are replaced with the colons. '${line/,/:}' will replace only the 1st match. The extra slash in '${line//,/:}' will replace all the matches. Note: This method will work in bash and ksh93 or higher, not in all flavors.


1 Answers

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:

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

Or with awk:

$ awk '1' RS=, <<< "$var"
hi
hello how
are you
doing
like image 127
fedorqui 'SO stop harming' Avatar answered Oct 27 '22 23:10

fedorqui 'SO stop harming'