Ubuntu – Replace a pattern containing an apostrophe within a file

awkcommand linesedtext processing

I have a text file which contains this line:

set stime = '0 0'

I need to change that to:

set stime = '0 4'

I tried these commands:

sed -i 's/original/new/g' file.txt

and

awk '{gsub(/pattern/,"replacement")}' file 

but they don't work. I guess it is because of the apostrophe in the pattern. How can I do this correctly?

Best Answer

In order to escape single quote ', you should use double quotes: ".

In order to update in-place you should use sed -i

The following code should update in-place, and escape the single quote:

sed -i "s/set stime = '0 0'/set stime = '0 4'/g" input_file_name