replacement for cut --output-delimiter

百般思念 提交于 2019-12-13 13:00:34

问题


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.


回答1:


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



回答2:


Perhaps do it in bash 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


来源:https://stackoverflow.com/questions/20195728/replacement-for-cut-output-delimiter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!