Easy Bash Cut 分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12219493/
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
Easy Bash Cut Delimiter
提问by Wai Chin
I have this..
我有这个..
$input = "echo a b c d"
echo -e "$input" | cut -d " " -f 2-
but I just want a simple cut that will get rid of echo as well as print
但我只想要一个简单的剪裁,它可以消除回声和印刷
a b c d #(single space) only
回答by Silox
echo -e "$input" | tr -s ' ' | cut -d " " -f2-
Also gets rid of the 'echo'.
也摆脱了'回声'。
回答by ghoti
You don't need any tools besides what bash provides built-in.
除了 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: you avoid the extra overhead of pipes.
好处:您可以避免管道的额外开销。
Down-side: you need to assign an extra variable, because you can't do complex pattern expansion within complex pattern expansion (i.e. echo ${${input//? / }#* }won't work).
缺点:您需要分配一个额外的变量,因为您不能在复杂模式扩展中进行复杂模式扩展(即echo ${${input//? / }#* }不起作用)。
回答by chepner
A little roundabout, but interesting:
有点迂回,但很有趣:
( set -- $input; shift; echo $@ )
回答by perreal
With sed:
使用 sed:
sed -e 's/[ ]*[^ ]*[ ]*\(.*\)//' -e 's/[ ]*/ /g' -e 's/^ *//' input_file

