2013-11-25 18 views
5

我創建的使用替代切--output定界符

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] 

我檢查了手冊頁「切割」唉,沒有「--output分隔符」在那裏!

那麼,如何在我的Solaris 10(bash)的?我想AWK會是一個解決辦法實現這一點,但我無法正確地架起的選項。

注意:逗號相互分離的變量可能具有「」空間。

+0

如果您想在RHEL上使用相同的cut命令,請安裝[GNU coreutils](https://www.gnu.org/software/coreutils/)軟件包。 – alanc

回答

7

怎麼樣使用tr這個?

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

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

隨着

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

或者與

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

不知何故,我感覺就像一個大傻瓜!我從來沒有想過使用'tr'或'sed'...... pfffttt .....非常感謝您的回答! – Marcos

+0

'sed的' 沒有工作, 的bash-3.00#$回聲VAR | sed的-e 'S /,/ \ N/G' hinhello hownare youndoing 但 'TR' 的工作! – Marcos

+0

Uhms我沒有要測試的Solaris服務器,但可能在http://stackoverflow.com/questions/8991275/escaping-newlines-in-sed-replacement-string中可以找到一些線索。並且很好的閱讀'tr'對你來說很好:) – fedorqui

3

也許做它在本身?

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

感謝你的回答,但是我更喜歡更小的東西,我也不想爲此使用數組。 – Marcos