replace a word with string in a file using shell

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error replace a word with string in a file using shell on this date .

I have a file in which I have to convert the string “lock allpages” into STRING ” LOCATION ‘hdfs://LOCATION/DIRECTORY/TBL;’ ” . I tried sed but it’s throwing some error. can someone suggest the changes that I need to do. I tried below but it’s not working. and also can someone suggest how to use sed for replacing multiple strings at once

sed -i 's/lock allpages/LOCATION 'hdfs://LOCATION/DIRECTORY/TBL';'


You need to escape your single quotes and forward slashes in your sed statement. .. That and you’re not COMPLETING the sed statement .. You need to close your second argument with a slash -> / (Or whatever delimiter you choose)

sed -i 's/lock allpages/LOCATION 'hdfs://LOCATION/DIRECTORY/TBL;'/g' 

OR if you put the statement in double quotes . There’s no need to escape the single quotes

sed -i "s/lock allpages/LOCATION 'hdfs://LOCATION/DIRECTORY/TBL;'/g" 

FURTHER — If you used a different delimiter .. Like a pipe | — There would be no need for escaping at all…

sed -i "s|lock allpages|LOCATION 'hdfs://LOCATION/DIRECTORY/TBL;'|g"