我试图在Redis中使用批处理sed替换每个密钥名称的批处理密钥重命名。(内部似乎没有更好的方法来执行此操作)。
redis-cli -n 5 KEYS "*::staging::*"| xargs -I {} echo "RENAME {} $(echo {} | sed 's/staging/development/g')" # Then pipe this command into redis-cli when working
我期望这样的输出: "RENAME redis::staging::key redis::development::key"
"RENAME redis::staging::key redis::development::key"
相反,我只是得到这个: "RENAME redis::staging::key redis::staging::key"
"RENAME redis::staging::key redis::staging::key"
这echo {} $(echo {}| sed 's/old/new/')就是让我挂起来的…如何正确地获取xargs输出并回显该输出的原始版本和编辑版本?$(echo {} |sed)应该工作吧?(如果是这样,我该如何正确执行?)
echo {} $(echo {}| sed 's/old/new/')
xargs
$(echo {} |sed)
sed本身能够产生未修改的行和已修改的行:
$ echo "redis::staging::key" | sed 's/^/RENAME /; p; s/staging/development/g' RENAME redis::staging::key RENAME redis::development::key
在上面,sed首先将RENAME字符串添加到该行的开头。然后,p命令告诉sed打印当时的行(“ staging”仍在其中)。下一个替换项放入“ development”,然后也打印该版本。
p
更新: 假设我们要在一行上输出:
$ echo "redis::staging::key" | sed 's/.*/RENAME & &/; s/staging/development/2' RENAME redis::staging::key redis::development::key
s上面的第一个命令将RENAME添加到开头,然后将其加倍。第二个阶段用开发替换了第二个阶段的出现。
s
xargs -I {} echo "RENAME {} $(echo {} | sed 's/staging/development/g')"
在执行xargs之前,bash处理字符串。特别是,它看到$(echo {} | sed 's/staging/development/g')并执行它(“命令替换”)并获得结果{}。因此,当xargs最终运行时,它会看到以下命令:
$(echo {} | sed 's/staging/development/g')
{}
xargs -I {} echo "RENAME {} {}"
因此,s/staging/development/g永远不会进行替换。
s/staging/development/g
有一个解决方法:
$ echo "redis::staging::key" | xargs -I {} sh -c 'echo RENAME {} $(echo {} | sed 's/staging/development/g')' RENAME redis::staging::key redis::development::key
上面将bash命令放在单引号内,并将它们作为参数传递给sh。这样,直到xargs进行替换后,外壳程序才处理字符串。
sh