gpt4 book ai didi

bash - 使 sed 就地替换文件中的文本,并在屏幕上显示替换的行

转载 作者:行者123 更新时间:2023-11-29 09:32:31 24 4
gpt4 key购买 nike

find . -type f -name "test.php" -print0 | xargs -0 egrep -sHnl "&\s*new\s"| xargs gsed -i.bak 's/&\(\s*new\s\)/\1/gi' -

这可以很好地替换所选文件。
但是我想同时查看原始文件和替换文件之间的区别。

find . -type f -name "test.php" -print0 | xargs -0 egrep -sHnl "&\s*new\s"| xargs gsed -i.bak 's/&\(\s*new\s\)/\1/gip' -

此尝试失败,但也复制了被替换的行。

find . -type f -name "test.php" -print0 | xargs -0 egrep -sHnl "&\s*new\s"| xargs gsed -n 's/&\(\s*new\s\)/\1/p' -

这可以工作,但既不会覆盖原始文件(即不会用替换的文本更改它),也不会备份。

this灵感,我试过跟随,但没有用。

find . -type f -name "test.php" -print0 | xargs -0 egrep -sHnl "&\s*new\s"| xargs gsed -i.bak 's/&\(\s*new\s\)/\1/gip;s/.*\n//' -

我怎样才能实现它?

最佳答案

好的老板,

也许 sed 的 w 选项可以提供帮助。这是一个开始;

[root@volte1 test]# cat scr.sh
sed -i '/ligne/{
s//line/g
w /dev/stdout
}' file.txt



[root@volte1 test]# cat file.txt
line1
line2
line3
line4
ligne5
line6
line7


[root@volte1 test]# ./scr.sh
line5


[root@volte1 test]# cat file.txt
line1
line2
line3
line4
line5
line6
line7

同时尝试以下输出文件之前/之后的差异:

$ cat scr2.sh
find . -name file.txt -printf '\n%p:\n' -exec sed -i '/ligne/{
h
s//line/g
H
x
s/\n/ >>> /
w /dev/fd/2
x
}' {} \;

关于bash - 使 sed 就地替换文件中的文本,并在屏幕上显示替换的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49256256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com