我有一个包含
之类的行的文件https://abcdefgh.com/123/pqrst/456/xyz.html
所以我想在该文件中搜索此行并将xyz.html
替换为mno.html
将shell脚本中的mno.html
作为输入。
怎么做?
答案 0 :(得分:2)
$ 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
答案 1 :(得分:0)
您可以使用此sed
,
sed '/https:\/\/abcdefgh.com\/123\/pqrst\/456\/xyz.html/s#\(.*\/\)\(.*\)#\1mno.html#g' yourfile
答案 2 :(得分:0)
使用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