简体   繁体   中英

Linux find and replace: how to escape single quotes in strings?

Total newbee on this, so here's what is not working:

find . -type f -exec sed -i 's/\$_SESSION['valOne']"/\$valTwo,"/g' {} +

While it should replace recursively, in all the directories it is going through:

$_SESSION['valOne'] into just: $valTwo

I've done this many times, but it seems the quotes are too confusing for me. Any help here is highly appreciated.

如果我了解您正在尝试做什么(字面替换),那么您需要单引号字符串,以便 shell 不会尝试解释其内容,然后对字符串中的每个单引号使用'\\''并转义任何BRE 元字符(即[]在您的情况下):

find . -type f -exec sed -i 's/$_SESSION\['\''valOne'\''\]"/$valTwo,"/g' {} +

Please try as follows. Change the ' -> " and escape with the backslash.

Example:

 find . -type f -exec sed -i "s/\$_SESSION\['valOne'\]/\$valTwo/g" {} +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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