我用的是Ubuntu Natty。
我正在尝试使用命令行perl编辑一个文本文件。下面是我的代码片段:
path_to_php_exec="/usr/local/php/bin/php"
path_to_php_prog="/root/run/php_gearman_worker.php"
perl -0777 -i -pe "s/(command[ ]*=[ ]*)[^\n]+/\${1}=$path_to_php_exec $path_to_php_prog\n/g" /etc/supervisord_program_gearman.conf但是,当我运行这个命令时,我得到了以下错误
Bareword found where operator expected at -e line 1, near "s/(command[ ]*=[ ]*)[^\n]+/${1}=/usr"
Backslash found where operator expected at -e line 1, near "php\"
syntax error at -e line 1, near "s/(command[ ]*=[ ]*)[^\n]+/${1}=/usr"
Execution of -e aborted due to compilation errors.我有一种感觉,这与我的shell变量中的正斜杠有关,但我不太确定。在某种程度上,它还是一个命令行脚本的新手。
如果能帮上忙我会很感激。
谢谢。
发布于 2012-05-18 03:22:51
是的,就是这样。您可以为Perl的s///命令选择不同的分隔符。另外,您的正则表达式是面向行的,所以为什么不逐行解析文件呢?
perl -i -pe \
"$(printf 's{(command\s*=\s*).*}{$1 %s %s}g' "$path_to_php_exec" "$path_to_php_prog")" \
/etc/supervisord_program_gearman.conf我假设您不想在输出文件中使用两个等号,所以我将其从替换块中删除。
https://stackoverflow.com/questions/10641712
复制相似问题