bash 删除文本流中的第一个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7814205/
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
Remove First Word in text stream
提问by Trcx
How would I remove the first word from each line of text in a stream? i.e.
如何从流中的每一行文本中删除第一个单词?IE
$cat myfile
some text 1
some text 2
some text 3
what I want is
我想要的是
$cat myfile | magiccommand
text 1
text 2
text 3
How would I go about this using bash? I could use awk '{print $2 $3 $4 $5 ....}' but that's messy and would result in extra spaces for all null arguments. I was thinking that sed might be able to do this, but I could not find any examples of this. Any help is appreciated! Thanks!
我将如何使用 bash 解决这个问题?我可以使用 awk '{print $2 $3 $4 $5 ....}' 但这很混乱并且会导致所有空参数的额外空间。我在想 sed 可能能够做到这一点,但我找不到任何这样的例子。任何帮助表示赞赏!谢谢!
回答by Kent
based on your example text,
根据您的示例文本,
cut -d' ' -f2- yourFile
should do the job.
应该做的工作。
回答by Yanick Girouard
That should work:
那应该工作:
$ cat test.txt
some text 1
some text 2
some text 3
$ sed -e 's/^\w*\ *//' test.txt
text 1
text 2
text 3
回答by vk239
Here is a solution using awk
这是使用的解决方案 awk
awk '{= ""; print $ cat myfile
some text 1
some text 2
some text 3
$ cat myfile | sed 's/[^ ]* *//'
text 1
text 2
text 3
}' yourfile
回答by user973254
run this sed "s/^some\s//g" myfile
you even don't need to use a pipe
运行这个sed "s/^some\s//g" myfile
你甚至不需要使用管道
回答by Benny
To remove the first word, until space no matter how many spaces exist, use: sed 's/[^ ]* *//'
要删除第一个单词,直到空格为止,无论存在多少空格,请使用: sed 's/[^ ]* *//'
Example:
例子:
##代码##