[SOLVED] Replace string in a file with url present in another file in shell script


I was like trying to replace a string in a file with the url present in another file using sed command.
for example…
let url.txt be the file that contains url:


and demo.txt contains
the sed command I used is:

sed -i "s/Replace_Url/$(sed 's:/:\\/:g' url.txt)/" demo.txt

there comes no error but the string hasn’t been replaced..


It’d be hard to do this job robustly with sed since sed doesn’t support literal string operations (see Is it possible to escape regex metacharacters reliably with sed), so just use awk instead, e.g.:

awk -v old='Replace_Url' '
    NR==FNR { new=$0; next }
    s=index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+length(old)) }
    { print }
' url.txt demo.txt

Answered By – Ed Morton

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *