Easy Bash Cut Delimiter

时间:2012-08-31 16:48:24

标签: bash

我有这个..

$input = "echo     a       b                c   d"
echo -e "$input" | cut -d " " -f 2-

但我只想要一个简单的剪辑,它将摆脱回声以及打印

a b c d #(single space) only

4 个答案:

答案 0 :(得分:5)

echo -e "$input" | tr -s ' ' | cut -d " " -f2-

还摆脱了'回声'。

答案 1 :(得分:4)

除了bash提供的内置功能外,您不需要任何工具。

[ghoti@pc ~]$ input="echo     a       b                c   d"
[ghoti@pc ~]$ output=${input//  / }
[ghoti@pc ~]$ echo $output
echo a b c d
[ghoti@pc ~]$ echo ${output#* }
a b c d
[ghoti@pc ~]$ 

Up-side:你可以避免额外的管道开销。

下方:你需要分配一个额外的变量,因为你不能在复杂的模式扩展中进行复杂的模式扩展(即echo ${${input//  / }#* }不起作用)。

答案 2 :(得分:3)

有点迂回,但有趣:

( set -- $input; shift; echo $@ )

答案 3 :(得分:1)

使用sed:

sed -e 's/[ ]*[^ ]*[ ]*\(.*\)/\1/' -e 's/[ ]*/ /g' -e 's/^ *//' input_file