sed 替换单/双引号文本? [英] sed replace single/double quoted text?

查看:95
本文介绍了sed 替换单/双引号文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用 sed 替换某些单/双引号文本时遇到一些困难,想知道这两个示例的正确方法是什么

Having some difficulty in replacing some single/double quoted text with sed and was wondering what's the correct method for these 2 examples

更改 Memaced.ini 文件内容

to change Memached.ini file contents from

[server]
server[] = "localhost:11211"

[server]
server[] = "localhost:11211"
server[] = "localhost:11212"

并更改这些行的 memcache.php 文件内容

and to change memcache.php file contents for these lines from

define('ADMIN_USERNAME','username');    // Admin Username
define('ADMIN_PASSWORD','password');    // Admin Password

$MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

define('ADMIN_USERNAME','myusername');  // Admin Username
define('ADMIN_PASSWORD','mypassword');      // Admin Password

$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
$MEMCACHE_SERVERS[] = 'localhost:11212'; // add more as an array

我试过例如

sed -i 's/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','u'/g' /var/www/html/memcache.php

当命令运行时,memcache.php 文件根本没有改变?

while command runs, memcache.php file isn't changed at all ?

推荐答案

您可以将 sed 命令中的单引号替换为双引号单引号.shell 将单引号视为字符串的结尾.所以,让它吧.你有

You can replace the single quotes in the sed command with double-quoted single quotes. The shell sees a single quote as ending a string. So, let it. You had

sed -i 's/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','u'/g' /var/www/html/memcache.php

但是,如果将 sed 命令中的 ' 替换为 '"'"',那么 shell 会将第一个 ' 视为第一个单引号字符串的结尾,然后将 "'" 视为双引号单引号,并且然后最后一个 ' 作为新的单引号字符串的开头.那是

But, if you replace the ' in the sed command with '"'"', then shell will see the first ' as ending the first single-quoted string, then "'" as a double-quoted single quote, and then the last ' as a beginning of a new single-quoted string. That'd be

sed -i 's/'"'"'ADMIN_USERNAME'"'"','"'"'memcache'"'"'/'"'"'ADMIN_USERNAME'"'"','"'"'u'"'"'/g' /var/www/html/memcache.php

出于同样的原因,您还应该能够在命令中使用 '\'' 代替 '.

You should also be able to do '\'' in place of the ' within the command, for the same reason.

sed -i 's/'\''ADMIN_USERNAME\'',\''memcache\''/\''ADMIN_USERNAME\'',\''u\''/g' /var/www/html/memcache.php

但实际上,最好使用替代机制.我建议将源字符串和目标字符串定义为变量,然后将它们放入 sed 字符串中.

But really, it'd be better to use an alternative mechanism. I'd suggest defining the source and target strings as variables, and then put those in the sed string.

SRC="'ADMIN_USERNAME','memcache'"
DST="'ADMIN_USERNAME','u'"
sed -i "s/$SRC/$DST/g" /var/www/html/memcache.php

这种方式更具可读性,并且它使您可以更轻松地以一口大小的块以理智的方式处理引用混乱.是的,shell 变量内容不受单词扩展的影响,除非你强迫它"知识.:)

That's way more readable, and it makes it easier for you to handle the quoting mess in a sane way with bite-sized chunks. Yay "shell variable contents aren't subject to word expansion unless you force it" knowledge. :)

不过,请确保不要在 $SRC 或 $DST 变量中放置/.;)

Make sure you don't put a / in the $SRC or $DST variables, though. ;)

这篇关于sed 替换单/双引号文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆