xyz*_*xyz 3 linux shell awk replace sed
这是我在学习sed过程中的第3篇文章.我有一个假设的要求.我希望能够用'was'替换每行中的第三个单词,其中单词由空格分隔.
bash$ cat words
hi this is me here
hi this is me again
hi this is me yet again
hi this is me
Run Code Online (Sandbox Code Playgroud)
期望的输出:
hi this was me here
hi this was me again
hi this was me yet again
hi this was me
Run Code Online (Sandbox Code Playgroud)
人们可以帮助解决如何使用sed.我尝试了一些执行指令,但没有奏效.谢谢,
Jagrati
我找到了!我找到了!
好的,我终于得到了正确的指示.这有效:
sed -e 's/[^ ]*[^ ]/was/3' words
Run Code Online (Sandbox Code Playgroud)
如果您不关心格式化,这是一种更简单的方法
$ 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
Run Code Online (Sandbox Code Playgroud)
我总是这样做:使用组匹配第一个和第二个单词,然后使用反向引用替换它们自己.
sed 's/^\([^ ]*\) \([^ ]*\) is/\1 \2 was/'
Run Code Online (Sandbox Code Playgroud)