Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Unix 使用变量sed替换整条线路_Unix_Sed - Fatal编程技术网

Unix 使用变量sed替换整条线路

Unix 使用变量sed替换整条线路,unix,sed,Unix,Sed,文件内容 something OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect something sed命令已尝试 URL='xyz-new.com' #This will be forming at run time sed -i 'abc /c\ OIDCRedirectURI $URL/newredirect' /etc/httpd/conf.d/proxy.conf 基本上我想用新的URL替换给定的URL 但它正在被$URL替

文件内容

something
OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect
something
sed命令已尝试

URL='xyz-new.com' #This will be forming at run time

sed -i 'abc /c\ OIDCRedirectURI $URL/newredirect' /etc/httpd/conf.d/proxy.conf
基本上我想用新的URL替换给定的URL

但它正在被$URL替换


有指针吗?

假设新URL保存在shell变量中:
$URL
这一行可以帮助您:

sed -i "s@\(^OIDCRedirectURI \).*@\1$URL/newredirect@" file
在您的示例中,
URL
没有协议,例如HTTP或https。如果要“重用”来自“旧”URL的协议前缀,可以将其添加到捕获组:

sed -i "s@\(^OIDCRedirectURI http[^/]*//\).*@\1$URL/newredirect@" file
只是为了证明它是有效的:
我个人不会用sed来做这个

#!/bin/sh -x

mystring="OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect"
redir=$(echo -e "${mystring}" | cut -d' ' -f1)
oldurl=$(echo -e "${mystring}" | cut -d' ' -f2)
newurl="http://xyz-new.com"

echo -e "${redir} ${newurl}"

当然,您可能希望在循环中使用这些条目的列表来执行此操作,但这不会太困难。您只需将旧URL和新URL放在两个堆栈文件中,并确保它们在每个堆栈文件中的顺序正确。

使用双引号而不是单引号。另外,“赋值”
$URL='…'
实际上是一个命令。改为写
url='…'
。我推荐。您的sed替换语法不正确。请在您的文件中显示一行作为示例,在之前和之后。@Kent我更新了问题,以便您现在更加清楚。@Socowi双引号在我的案例中不起作用。我更新了问题,让你更清楚。在更新的问题中,你仍然使用单引号
而不是双引号
。除此之外,您的主要问题是Kent指出的
sed
命令。修复引号只是第一步,它给了我一个错误:没有这样的文件或目录。conf.d/proxy.conf,这是因为我需要传递文件-/etc/httpd/conf.d/proxy.conf的完整路径。文件路径/正在与sed命令/@DeveshAgrawal混合不,不会。sed中
s
的分隔符是
@
,而
/
不是问题。“一定是别的原因。”德夫沙格拉沃在回答中检查了测试。谢谢!最后,它似乎起了作用。我这边肯定有问题。
#!/bin/sh -x

mystring="OIDCRedirectURI http://abc-mt.tc.ac.com/newredirect"
redir=$(echo -e "${mystring}" | cut -d' ' -f1)
oldurl=$(echo -e "${mystring}" | cut -d' ' -f2)
newurl="http://xyz-new.com"

echo -e "${redir} ${newurl}"