gpt4 book ai didi

linux - 无法使用 sed 将文本替换为 shell 变量

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

出于某种原因,下面帖子中的答案对我不起作用。有什么想法吗?
how to use sed to replace a string in a file with a shell variable

我运行的是 CentOS 6.5

`NEW="new value"
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}'
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW'
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s'

最佳答案

尝试将 s 从 sed 中去掉,例如

$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three

要获得更完整的答案,请尝试 this answer

关于linux - 无法使用 sed 将文本替换为 shell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46694048/

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