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

发布于 2024-12-10 12:39:18 字数 315 浏览 4 评论 0原文

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

例如,

$ cat myfile
some text 1
some text 2
some text 3

我想:

$ cat myfile | magiccommand
text 1
text 2
text 3

我将如何使用 Bash 来解决这个问题?我可以使用 awk '{print $2 $3 $4 $5 ....}',但这很混乱,并且会导致所有空参数都有额外的空格。我想 sed 也许能够做到这一点,但我找不到任何这样的例子。

How would I remove the first word from each line of text in a stream?

For example,

$ cat myfile
some text 1
some text 2
some text 3

I want:

$ 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲娇萝莉攻 2024-12-17 12:39:18

根据您的示例文本,

cut -d' ' -f2- yourFile

应该可以完成这项工作。

Based on your example text,

cut -d' ' -f2- yourFile

should do the job.

耳根太软 2024-12-17 12:39:18

那应该有效:

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

$ sed -e 's/^\w*\ *//' test.txt
text 1
text 2
text 3

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
-柠檬树下少年和吉他 2024-12-17 12:39:18

这是使用 awk 的解决方案

awk '{$1= ""; print $0}' yourfile 

Here is a solution using awk

awk '{$1= ""; print $0}' yourfile 
萌酱 2024-12-17 12:39:18

运行这个:

sed "s/^some\s//g" myfile

您甚至不需要使用管道。

Run this:

sed "s/^some\s//g" myfile

You even don't need to use a pipe.

紫轩蝶泪 2024-12-17 12:39:18

要删除第一个单词,直到出现空格(无论存在多少个空格),请使用: sed 's/[^ ]* *//'

示例:

$ cat myfile 
some text 1
some  text 2
some     text 3

$ cat myfile | sed 's/[^ ]* *//'
text 1
text 2
text 3

To remove the first word, until space no matter how many spaces exist, use: sed 's/[^ ]* *//'

Example:

$ cat myfile 
some text 1
some  text 2
some     text 3

$ cat myfile | sed 's/[^ ]* *//'
text 1
text 2
text 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文