1

/home/temp.txt

http://url_new_address.com

/home/list.txt

URL http://url_address.com:URL1
URL http://url_address.com:URL2
URL http://url_address.com:URL3

How can I replace the line in temp.txt with URL2 in list.txt with shell script ?

after command

/home/list.txt

URL http://url_address.com:URL1
URL http://url_new_address.com:URL2
URL http://url_address.com:URL3
tioma
  • 13
  • 2

1 Answers1

1

Thanks to don_crissti's suggestion and Stéphane Chazelas solution I have come up with the following:

#!/bin/sh

lhs=':URL2'
rhs="URL $(cat temp.txt):URL2"
escaped_lhs=$(printf '%s\n' "$lhs" | sed 's:[][\/.^$*]:\\&:g')
escaped_rhs=$(printf '%s\n' "$rhs" | sed 's:[\/&]:\\&:g;$!s/$/\\/')

sed "s/.*$escaped_lhs/$escaped_rhs/" list.txt

This will find any line containing ending in :URL2 and replace it with:

URL http://url_new_address.com:URL2
jesse_b
  • 37,005