SED和变量的小问题。

时间:2021-08-04 17:06:57

Hey guys and girls.

嘿男孩和女孩。

I'm new here, please be kind :). Yeah, i got some problem... I was writing some script and always used the absolute path to my document folder. Some stuff like this:

我是新来的,请见谅。是的,我有问题……我正在编写一些脚本,并且总是使用文档文件夹的绝对路径。一些东西是这样的:

cd /home/myname/documents/blabla/

More-or-les finished, i wanted to clear up the code, make it available for others and change it to something like this:

差不多完成后,我想清理代码,让其他人可以使用它,并将其更改为如下内容:

VARIABLE="/home/myname/documents"
cd $VARIABLE/blabla

And because there are over 9000 absolute paths in the script, i naturally thought about using SED to "clean up" my script. And here comes the problem:

由于脚本中有超过9000条绝对路径,我自然地考虑使用SED“清理”我的脚本。问题来了:

sed -ie "s|home/myname/documents|$VARIABLE|g" script1

This gives me of course empty spaces. I want to exchange the path with the string "$VARIABLE" and not with the Variable itself. Tried out some stuff like quotation marks, some brackets but couldn't figure out how it could work.

这当然给了我空间。我想用字符串“$VARIABLE”来交换路径,而不是用变量本身。尝试了一些东西,比如引号,一些括号,但不知道如何工作。

So i apply to you. Could you help me? I could really take some sleep...^^

所以我向你申请。你能帮我吗?我真的可以带一些睡眠…^ ^

Thanks alot and sorry for my english. A n00bie.

非常感谢,也很抱歉我的英语。n00bie。

2 个解决方案

#1


3  

Just change " to ' to not expand variable but treat the text literally:

只需将“to”改为“not expand variable”,而将文本按字面意思处理:

sed -ie 's|home/myname/documents|$VARIABLE|g' script1

EDIT: Be careful about the initial / too, just so you don't miss that your regexp doesn't remove that.

编辑:对初始/也要小心,这样你就不会错过你的regexp不删除它。

#2


0  

sed -ie "s#home/myname/documents#\$VARIABLE#g" script1

alternative, escape the $

选择,逃避美元

#1


3  

Just change " to ' to not expand variable but treat the text literally:

只需将“to”改为“not expand variable”,而将文本按字面意思处理:

sed -ie 's|home/myname/documents|$VARIABLE|g' script1

EDIT: Be careful about the initial / too, just so you don't miss that your regexp doesn't remove that.

编辑:对初始/也要小心,这样你就不会错过你的regexp不删除它。

#2


0  

sed -ie "s#home/myname/documents#\$VARIABLE#g" script1

alternative, escape the $

选择,逃避美元