假设我有这个文件:
cat > test.txt <<EOF
line one word
line two word
line three word
line one two word
EOF
And let's say I want to replace all of the words 'two' with 'TWO', inline in-place in the file test.txt
.
现在,我所做的通常是构建一个“预览”(使用-n
不打印行,然后用/p
- 仅打印匹配的行):
$ sed -n 's/two/TWO/gp' test.txt
line TWO word
line one TWO word
...然后我通常执行实际的就地替换(使用-i
,并且没有/p
):
$ sed -i 's/two/TWO/g' test.txt
$ cat test.txt
line one word
line TWO word
line three word
line one TWO word
有没有办法得到sed
在文件中就地更改行,and从单个命令行将更改的行打印到标准输出?
在 Linux 上,您也许可以摆脱:
sed -i '/two/{s/two/TWO/g; w /dev/stdout}' test.txt
在 BSD 系统(包括 Mac OS X)上,sed
关于何时可以将操作组合到一行上有稍微奇怪的规则,我不得不使用:
sed -i '/two/{s/two/TWO/g; w /dev/stdout
}' test.txt
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)