更新字符串我有一个包含像查找 - 使用shell脚本
https://abcdefgh.com/123/pqrst/456/xyz.html
行所以我想搜索这条线在该文件中,并与mno.html
xyz.html
文件 - 替换
将在shell脚本中将mno.html
作为输入。
如何做到这一点?
更新字符串我有一个包含像查找 - 使用shell脚本
https://abcdefgh.com/123/pqrst/456/xyz.html
行所以我想搜索这条线在该文件中,并与mno.html
xyz.html
文件 - 替换
将在shell脚本中将mno.html
作为输入。
如何做到这一点?
您可以使用此sed
,
sed '/https:\/\/abcdefgh.com\/123\/pqrst\/456\/xyz.html/s#\(.*\/\)\(.*\)#\1mno.html#g' yourfile
使用awk
如果行是完全一样的例子,我的意思是如果没有其他字符之前或之后
awk '{print gensub(/^(https:\/\/abcdefgh.com\/123\/pqrst\/456\/)xyz.html$/,"\\1mno.html","g")}' input.txt
否则:
awk '{print gensub(/(https:\/\/abcdefgh.com\/123\/pqrst\/456\/)xyz.html/,"\\1mno.html","g")}' input.txt
你应该提到这是GNU awk特有的。您应该在'.com'和'.html'中跳过'.'。同样值得告诉OP,他们需要避开可能出现在文件名中的其他可能的RE元字符等(例如'*')。说实话,虽然我只是使用sed,如果我要使用RE替换方法,并且不得不逃脱一堆字符。 –
$ awk 'BEGIN{FS=OFS="/"} index($0,"https://abcdefgh.com/123/pqrst/456/xyz.html"){$NF="mno.html"} 1' file
https://abcdefgh.com/123/pqrst/456/mno.html
或者如果这两个值已经存储在shell变量中:
$ old="https://abcdefgh.com/123/pqrst/456/xyz.html"
$ new="mno.html"
$ awk -v old="$old" -v new="$new" 'BEGIN{FS=OFS="/"} index($0,old){$NF=new} 1' file
https://abcdefgh.com/123/pqrst/456/mno.html
您应该在'.com'和'.html'中跳过'.'。同样值得告诉OP,他们需要避开可能出现在文件名中的其他可能的RE元字符等(例如'*')。 –