bash 如何在bash中删除字符串的第一部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13788166/
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
How to remove the first part of a string in bash?
提问by Hard Rain
This code will give the first part, but how to remove it, and get the whole string without the first part?
这段代码将给出第一部分,但如何删除它,并在没有第一部分的情况下获取整个字符串?
echo "first second third etc"|cut -d " " -f1
回答by sleepsort
You should have a look at info cut
, which will explain what f1
means. Also, a same question here: question-7814205
你应该看看info cut
,它会解释什么f1
意思。另外,这里也有同样的问题:question-7814205
Actually we just need fields after(and) the second field. -f
tells the command to search by field, and 2-
means the second and following fields.
实际上我们只需要在(和)第二个字段之后的字段。-f
告诉命令按字段搜索,2-
表示第二个和后面的字段。
echo "first second third etc" | cut -d " " -f2-
回答by Mat
You can use substring removalfor that, no need for external tools:
您可以为此使用子字符串删除,无需外部工具:
$ foo="a b c d"
$ echo "${foo#* }"
b c d
回答by Aamir
You can do:
你可以做:
echo "first second third etc" | cut -d " " -f2-
>> second third etc
回答by Gilles Quenot
Try doing this :
尝试这样做:
echo "first second third etc"|cut -d " " -f2-
It's explained in
它在
man cut | less +/N-
N- from N'th byte, character or field, to end of line
N- 从第 N 个字节、字符或字段到行尾
As far of you have the bashtag, you can use bash parameter expansionlike this :
就你有bash标签而言,你可以像这样使用bash 参数扩展:
x="first second third etc"
echo ${x#* }
回答by Rahul Tripathi
Try this:-
尝试这个:-
echo "first second third etc"|cut -d " " -f2-