bash 替换 cut --output-delimiter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20195728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:45:34  来源:igfitidea点击:

replacement for cut --output-delimiter

bashawksolariscut

提问by Marcos

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.

在 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

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

但不幸的是,当我在 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]

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

我检查了'cut'的手册页,可惜那里没有'--output-delimiter'!

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.

那么我如何在 Solaris 10 (bash) 中实现这一点?我想 awk 将是一个解决方案,但我无法正确构建选项。

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

注意:逗号分隔的变量中可能有 " " 空格。

采纳答案by fedorqui 'SO stop harming'

What about using trfor this?

tr这个怎么样?

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

or

或者

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

With sed:

使用sed

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

Or with awk:

或者使用awk

$ awk '1' RS=, <<< "$var"
hi
hello how
are you
doing

回答by iruvar

Perhaps do it in bashitself?

也许在bash本身中做到这一点?

var="hi,hello how,are you,doing"
printf "$var" | (IFS=, read -r -a arr; printf "%s\n" "${arr[@]}")
hi
hello how
are you
doing