如何使用 sed 替换一行中的第三个单词

发布于 2024-09-10 12:17:16 字数 515 浏览 4 评论 0原文

这是我学习 sed 过程中的第三篇文章。我有一个假设的要求。我希望能够用“was”替换每行中的第三个单词,其中单词由空格分隔。

bash$ cat words
hi this is me here
hi this   is me again
hi   this   is me yet again
 hi  this   is     me

期望的输出:

hi this was me here
hi this   was me again
hi   this   was me yet again
 hi  this   was     me

请大家帮忙了解一下如何使用 sed 来实现这一点。我尝试了一些执行指令,但没有成功。 谢谢,

贾格拉蒂

,我找到了!我找到了!

好吧,我终于得到了正确的指示。这有效:

sed -e 's/[^ ]*[^ ]/was/3' words

This is my 3rd post in the process of learning sed. I have a hypothetical requirement. I want to be able to replace 3rd word in each line by 'was', where words are delimited by space(s).

bash$ cat words
hi this is me here
hi this   is me again
hi   this   is me yet again
 hi  this   is     me

Desired output:

hi this was me here
hi this   was me again
hi   this   was me yet again
 hi  this   was     me

Could people please help with how to do it with sed. I tried a few executing instructions, but didn't work.
Thanks,

Jagrati

I found it! I found it!

Okay, I got the right instruction at last. This works:

sed -e 's/[^ ]*[^ ]/was/3' words

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

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

发布评论

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

评论(4

野侃 2024-09-17 12:17:16

如果您不关心格式,这是一种更简单的方法

$ awk '{$3="was"}1' file
hi this was me here
hi this was me again
hi this was me yet again
hi this was me

if you do not care about formatting, this is a simpler approach

$ awk '{$3="was"}1' file
hi this was me here
hi this was me again
hi this was me yet again
hi this was me
尬尬 2024-09-17 12:17:16

我总是这样做:使用匹配第一个和第二个单词,然后使用反向引用将它们替换为自身。

sed 's/^\([^ ]*\) \([^ ]*\) is/\1 \2 was/'

I always do it like this: match the first and second word using groups, and then replace them with themselves using a backreference.

sed 's/^\([^ ]*\) \([^ ]*\) is/\1 \2 was/'
江城子 2024-09-17 12:17:16

这会考虑单词边界而不仅仅是空格,因此当有标点符号时它也有效。它需要 GNU sed

$ cat words
hi this is me here
hi this   is me again
hi   this   is me yet again
 hi  this   is     me
 hi   this   "is,  me
$ sed 's/\w\+\(\W\)/was\1/3' words
hi this was me here
hi this   was me again
hi   this   was me yet again
 hi  this   was     me
 hi   this   "was,  me

This looks at word boundaries rather than only whitespace so it works when there is punctuation, too. It requires GNU sed:

$ cat words
hi this is me here
hi this   is me again
hi   this   is me yet again
 hi  this   is     me
 hi   this   "is,  me
$ sed 's/\w\+\(\W\)/was\1/3' words
hi this was me here
hi this   was me again
hi   this   was me yet again
 hi  this   was     me
 hi   this   "was,  me
久而酒知 2024-09-17 12:17:16

在任何类型的 sed 中:

sed 's/^\([[:blank:]]*[^[:blank:]]\{1,\}[[:blank:]]\{1,\}[^[:blank:]]\{1,\}[[:blank:]]\{1,\}\)[^[:blank:]]\{1,\}\(.*\)$/\1was\2/' words

In any type of sed:

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