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

1 Answer 1

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
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.