删除文本流中的第一个单词

Trc*_*rcx 27 bash awk sed cat

如何从流中的每行文本中删除第一个单词?即

$cat myfile 
some text 1
some text 2
some text 3
Run Code Online (Sandbox Code Playgroud)

我想要的是什么

$cat myfile | magiccommand 
text 1
text 2
text 3
Run Code Online (Sandbox Code Playgroud)

我怎么用bash来解决这个问题呢?我可以使用awk'{print $ 2 $ 3 $ 4 $ 5 ....}'但这很麻烦,会导致所有空参数的额外空格.我当时认为sed可能会这样做,但我找不到任何这方面的例子.任何帮助表示赞赏!谢谢!

Ken*_*ent 64

根据您的示例文本,

cut -d' ' -f2- yourFile
Run Code Online (Sandbox Code Playgroud)

应该做的工作.

  • 注意:我尝试使用它来删除“history”输出的索引,但由于较小数字之前的空格充当分隔符,因此它在历史开始时不起作用。 (3认同)

Yan*_*ard 12

这应该工作:

$ cat test.txt
some text 1
some text 2
some text 3

$ sed -e 's/^\w*\ *//' test.txt
text 1
text 2
text 3
Run Code Online (Sandbox Code Playgroud)


vk2*_*239 7

这是一个使用的解决方案 awk

awk '{$1= ""; print $0}' yourfile 
Run Code Online (Sandbox Code Playgroud)

  • 这将在每行的开头留下一个空格 (2认同)

use*_*254 5

运行它sed "s/^some\s//g" myfile甚至不需要使用管道