我创建了一个正在使用的脚本
cut -d',' -f- --output-delimiter=$'\n'
在 RHEL 5 中为每个命令分隔值添加一个换行符,例如
[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
但不幸的是,当我在 Solaris 10 中运行相同的命令时,它根本不起作用:( !
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]
我检查了手册页中的“cut”,可惜那里没有“--output-delimiter”!
那么我如何在 Solaris 10 (bash) 中实现这一点?我想 awk 会是一个解决方案,但我无法正确构建选项。
注意:逗号分隔的变量中可能有“”空格。
最佳答案
为此使用 tr
怎么样?
$ tr ',' '\n' <<< "$var"
hi
hello how
are you
doing
或
$ echo $var | tr ',' '\n'
hi
hello how
are you
doing
与 sed :
$ sed 's/,/\n/g' <<< "$var"
hi
hello how
are you
doing
或用awk :
$ awk '1' RS=, <<< "$var"
hi
hello how
are you
doing
关于bash - 替换 cut --output-delimiter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20195728/