gpt4 book ai didi

linux - Bash 脚本 : Restore specific text in a file after some file editing

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:14 24 4
gpt4 key购买 nike

这个问题与我在 Bash script: Appending text at the last character of specific line of a file 中提出的问题有关.

在包含唯一字符串 MYVERSION 的(文件)行末尾 append 一个变量后,我现在想恢复该行的初始内容。

我设法将该行的初始内容存储在一个名为 "OLD_MY_VERSION" 的变量中,但是当我尝试使用 sed 恢复该行时

-i "/^MYVERSION=/ c\$$OLD_MY_VERSION myfile.txt

我得到的是 "$MYVERSION=0.1" 而不是 "MYVERSION=0.1"(当然没有引号)。

知道如何删除多余的字符 ($) 吗?如果我不放入额外的 $,我不会在替换中得到 "OLD_MY_VERSION" 变量的内容,而只会得到 "$OLD_MY_VERSION" 字符串。

非常感谢。

最佳答案

我认为你应该试试这个:

sed -i "s/^MYVERSION=.*$/$OLD_MY_VERSION/" myfile.txt

这会将以 MYVERSION 开头的整行 (s) 替换为 $OLD_MY_VERSION 的内容。

您也可以使用 c 命令,但您不需要命令中的 \$:

sed -i "/^MYVERSION/ c$OLD_MY_VERSION" myfile.txt

关于linux - Bash 脚本 : Restore specific text in a file after some file editing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949094/

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