简体   繁体   English

使用sed替换文件中的字符串

[英]Replace string in files using sed

I do have the following sed command to replace one string with another in many files: 我确实有以下sed命令,可在许多文件中用一个字符串替换为另一个字符串:

sed -i 's/\/mnt\/disk-K\/99990_Analytics\/30000_OLAP\/31000_Cube\//\/home\/tgr\/Applications\/Saiku\/saiku-server\/tomcat\/webapps\/saiku\/WEB-INF\/classes\/foodmart\//g' /home/tgr/Applications/Saiku/tmp_data/data_sources/*

It should just replace one folder name with another and it works fine. 它应该只是将一个文件夹名称替换为另一个,并且可以正常工作。 Unfortunately I need to have variables instead of manually escaped strings in command. 不幸的是,我需要在命令中使用变量而不是手动转义的字符串。 I've tried: 我试过了:

ORIG_CUBES="/mnt/disk-K/99990_Analytics/30000_OLAP/31000_Cube/"
DEST_CUBES="/home/tgr/Applications/Saiku/saiku-server/tomcat/webapps/saiku/WEB-INF/classes/foodmart/"
TEMP_FOLDER="/home/tgr/Applications/Saiku/tmp_data"

sed -i "s\,$ORIG_CUBES,$DEST_CUBES,g" $TMP_FOLDER/data_sources/*

Unfortunately, this does not work. 不幸的是,这不起作用。 I've used "" instead of '' so variables are used and used , as separator so I do not need escape / . 我用“”,而不是'这样的变量的使用和使用,作为分隔符,所以我不需要越狱/

What am I missing here? 我在这里想念什么?

你可以试试看

sed -iE "s,$ORIG_CUBES,$DEST_CUBES,g" $TMP_FOLDER/data_sources/*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM