gpt4 book ai didi

linux - vim - 为什么搜索会找到它但不会搜索和替换? (此转义特殊字符模式)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:12 25 4
gpt4 key购买 nike

想在 vim 中搜索和替换,/find 找到模式但 :s%//g 不会?

有一个监控软件 raid 的脚本(如果有兴趣请查看 https://dwaves.org/2019/09/06/linux-server-monitor-software-raid-mail-notification-on-failure/)

echo "=== smart status of all drives ==="| tee -a /scripts/monitor/raid_status_mail.log

# want to search and replace the /path/to/file.sh with $LOGFILE

# searching for the pattern works like charm
/\/scripts\/monitor\/raid_status_mail.log

# but replacing it won't
:s%/\/scripts\/monitor\/raid_status_mail\.log/\$LOGFILE/g

# what does one do wrong?

应该用 $LOGFILE 替换/scripts/monitor/raid_status_mail.log

最佳答案

替换操作需要以 %s 为前缀,而不是相反作为 s%。这样做

%s/\/scripts\/monitor\/raid_status_mail\.log/\$LOGFILE/g

应该按预期工作。或者只是 Vim 在命令行模式下的等效 ex

printf '%s\n' "%s/\/scripts\/monitor\/raid_status_mail\.log/\$LOGFILE/g" w q | ex -s file

关于linux - vim - 为什么搜索会找到它但不会搜索和替换? (此转义特殊字符模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866507/

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