Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用bash替换url的一部分_Regex_Perl_Bash_Parsing_Url - Fatal编程技术网

Regex 用bash替换url的一部分

Regex 用bash替换url的一部分,regex,perl,bash,parsing,url,Regex,Perl,Bash,Parsing,Url,我正在使用一个bash脚本来替换文件中两个字符串之间的特定文本。看起来是这样的: GATEWAYURL = 'myDomain.com' CONFIGFILE = 'full/path/to/config.file' replacementString1="s/(?<=gatewayIp:).*(?=,)/\"${GATEWAYURL}\"/;" perl -pi -e $replacementString1 $CONFIGFILE gatewayIp:"the.old.domain

我正在使用一个bash脚本来替换文件中两个字符串之间的特定文本。看起来是这样的:

GATEWAYURL = 'myDomain.com'
CONFIGFILE = 'full/path/to/config.file'

replacementString1="s/(?<=gatewayIp:).*(?=,)/\"${GATEWAYURL}\"/;"

perl -pi -e $replacementString1 $CONFIGFILE
gatewayIp:"the.old.domain.name.com",
gatewayIp:"myDomain.com",
像这样:

GATEWAYURL = 'myDomain.com'
CONFIGFILE = 'full/path/to/config.file'

replacementString1="s/(?<=gatewayIp:).*(?=,)/\"${GATEWAYURL}\"/;"

perl -pi -e $replacementString1 $CONFIGFILE
gatewayIp:"the.old.domain.name.com",
gatewayIp:"myDomain.com",
一切都很好,但就我个人而言,我不知道该如何使用它来替换url的一部分。例如,我想要:

redirectUri: "http://the.old.domain.name.com/oauth2callback.html",
将是:

redirectUri: "http://myDomain.com/oauth2callback.html", 
我认为这是可行的:

replacementString1="s/(?<=redirectUri: \"http:\/\/).*(?=\/oauth2callback.html)/${GATEWAYURL}/;"

我尝试了许多其他方法来避开url中的//和/,但似乎无法使其正常工作。

您的问题是shell没有按照您期望的方式使用perl行

sed -i "s#http://[^/]*#http://$GATEWAYURL#" $CONFIGFILE 
按原样,它将替换replacementString1,然后将其解析为参数以传递给perl,因此perl的-e只将空格字符作为参数

要强制shell将其视为单个参数,只需添加双引号:

perl -pi -e "$replacementString1" $CONFIGFILE

您没有显示如何定义
GATEWAYURL
,但它显然包含未经扫描的斜杠。最简单的解决方法是使用文本中没有的分隔符@EranBen Natan的回答用sed
sed
演示了这一点,但是您可以在Perl中使用相同的语法。