简体   繁体   English

我需要使用SED将一个字符串替换为一个URL

[英]I need to replace a string using SED with an url

When I use a normal value like a=10 当我使用正常值,例如a=10

I am able to get the sed find/replace working 我可以让sed查找/替换工作

  sed -e "s/xxx/$a/g" a.txt

But when I try to use a variable like a=http://xyz.com 但是当我尝试使用像a=http://xyz.com这样a=http://xyz.com变量时

sed -e "s/xxx/$a/g" a.txt is not working... sed -e "s/xxx/$a/g" a.txt无法正常工作...

Its throwing unknown option error which I assume is due to the escape characters that are present over the variable 我认为它抛出未知选项错误是由于变量上存在转义字符

With awk your string can contain ANY character, including /, @, newline, whatever: 使用awk,您的字符串可以包含任何字符,包括/,@,换行符,无论如何:

$ a="http://xyz.com"
$ echo "<xxx>" | awk -v a="$a" '{gsub(/xxx/,a)}1
<http://xyz.com>

尝试这个:

sed -e "s@xxx@$a@g" a.txt 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM