Tuesday, October 25, 2022

[SOLVED] How to change a file's content using sed with multiple quotes and spaces in it?

Issue

So I'm working on a Ubuntu Docker image and have to replace a settng in a file using sed.

I'd like to change #$nrconf{restart} = 'i' into $nrconf{restart} = 'a'

The way I understand it is that I have to escape ', $, {, }. I then tried it using this sed command (tried with and without escaping #)

sed -i 's/#\$nrconf\{restart\}[[:space:]]=[[:space:]]\'i\'/\$nrconf\{restart\}[[:space:]]=[[:space:]]\'a\'/g' /etc/needrestart/needrestart.conf

But when I test the command it expects an input and when I run the command in a Docker image I get this error Syntax error: Unterminated quoted string

What am I missing? I think escaping the quotes is correct, I don't get it


Solution

Using sed

$ sed -E "s/^#($nrconf[^']*')[^']*/\1a/" input_file
$nrconf{restart} = 'a'


Answered By - HatLess
Answer Checked By - Willingham (WPSolving Volunteer)