1

So i have a line in a file that looks like this:

#multiline.pattern: ^\[

and im trying to use sed to replace it with this:

multiline.pattern: '^\['

and the command im using to do this is:

sudo sed -i 's/multiline.pattern: \^\\\[/multiline.pattern: '\'\^\\\\[\'/g' /etc/filename.txt

but everytime i do run this command i get this:

enter image description here

can someone please tell me what am i doing wrong here?

Thanks!

1
  • 3
    Welcome to the site. If at all possible, please avoid attaching screenshots of console output; copy-and-paste the output with code formatting instead, as screenshots are often difficult to read and contributors trying to investigate have to type-copy the text.
    – AdminBee
    Feb 4 '20 at 17:21
1

Given your example, something like this should work:

$ cat ex
#multiline.pattern: ^\[

$ sed -e "s/^#multiline\.pattern:.*/multiline.pattern: '^\\\['/" ex
multiline.pattern: '^\['

That replaces a line that starts with #multiline.pattern: followed by any number of characters to the end of the line with the pattern you're after.

The pattern you're after includes some special symbols (\, [), which I escaped with an additional \ (e.g., \\ gets you a literal \, \[ gets you a literal [).

0

you might work around that : sed "s@\^@\'\^@;s/$/\'/"

$ cat > toto
#multiline.pattern: ^\[
$ sed "s@\^@\'\^@;s/$/\'/" toto
#multiline.pattern: '^\['

Idea is to substitude ^ by '^ and then end of line by ' with end of line in two consecutive actions to simplify the regexp.

of course with sed -i ... it modifies the file

0

Where it's going wrong is the way the last part is quoted:

sed 's/multiline.pattern: \^\\\[/multiline.pattern: '\'\^\\\\[\'/g'
  # ^                                               ^             ^ ?

That last part is backslash escaped until the end where another single quote was opened. That's why bash gave the $PS2 prompt >, allowing you to type in more stuff until the quote is closed with another '

Remove that last ' and it will work:

sed 's/multiline.pattern: \^\\\[/multiline.pattern: '\'\^\\\\[\'/g
# or:
sed 's/multiline.pattern: ^\\\[/multiline.pattern: '"'^\\\['/g"
sed "s/multiline.pattern: ^\\\\\[/multiline.pattern: '^\\\['/g"
sed 's/multiline.pattern: ^\\\[/multiline.pattern: \x27^\\[\x27/g'

Also:

  • From your example output, it seems that you want to remove the # as well. To do so, include it in the matching pattern for sed (using whichever quoting style you prefer):
sed 's/#multiline...'
  • It's possible to use brackets to "catch" parts of your matching pattern, so that you can reuse those parts in the substitution (called subexpressions & back-references):
sed -r 's/#(multiline\.pattern: )(\^\\\[)/\1'\'\\2\'/g

(using -r / --regexp-extended isn't necessary for that, but it's more readable)

  • It's best to check that your sed command is functioning as intended by first running it without the -i / --in-place option so the output can be checked for errors, instead of overwriting the file with potential errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.