【发布时间】:2012-01-04 10:14:17
【问题描述】:
假设我有这个文件:
cat > test.txt <<EOF
line one word
line two word
line three word
line one two word
EOF
假设我想将文件test.txt中的所有单词“two”替换为“TWO”,inline in-place。
现在,我所做的通常是构建一个“预览”(使用-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 在文件中就地更改行,和从单个命令行将更改的行打印到标准输出?
【问题讨论】: