1

I want to remove a line from a file and understand this can be done with sed.

The command below fails because of unterminated address regex

sed -i '/$settings['file_temp_path'] = '../tmp';/d' file.php

Having read the the answer at unterminated address regex while using sed . I now understand this is because characters [ and / must be escaped.

Having tried this, the code below is still unsuccessful

sed -i '/$settings\['file_temp_path'] = '..\/tmp';/d' file.php

What is wrong with this? What am I missing?

beruffled
  • 21
  • 1
  • 3

2 Answers2

2

This might work for you (GNU sed):

sed -i '\#\$settings\['\''file_temp_path'\''\] = '\''\.\./tmp'\'';#d' file

When the regexp/replacement contains the match delimiter (usually /), it can either be escaped/quoted or the delimiters can be altered i.e. /\/tmp/ or \#/tmp#. Note that in the case of the substitution command s/\/tmp/replacement/ can also be s#/tmp#replacement# and leading delimiter does not need to escaped/quoted.

Meta characters i.e. ^,$,[,],*,.,\ and & must be escaped/quoted by \or placed in a character class e.g. . should be \. or [.].

As a rule of thumb, sed commands should be enclosed in single quotes ' and for single quotes to be included in the regexp they should be replaced by '\'' which closes off the existing commands, shell escapes/quotes a ' and reopens the next sed command.

Using double quotes " may also be used but may have unexpected side effects as they are open to shell interpolation.

N.B. If the regexp/substitution delimiter is put inside a character class it does not need to be escaped/quoted i.e. if / is the delimiter then [/] is the same as \/. Also note, that {,},|,? and + should not be escaped/quoted if they are to represent their literal value unless the -E or -r sed command line option is invoked, in which case they should be i.e + represents the plus sign as does \+ when the -E is invoked, whereas \+ and + when the -E or -r is invoked represent one or more of the preceding character/group.

potong
  • 55,640
  • 6
  • 51
  • 83
0

You need to escape several special characters in your pattern, including $.

Example content of file.php:

foo
$settings['file_temp_path'] = '../tmp';
bar

Example code:

$ sed -i "s/\$settings\['file_temp_path'\] = '..\/tmp';//" file.php                                                                        
$ cat file.php
foo

bar
Paolo
  • 21,270
  • 6
  • 38
  • 69
  • Thank you. I find this prints the whole file, and I do see the correct line has been removed, and an empty line space remains. But when viewing the content of the file, I find the line is still present. Is another step necessary to save the changes? – beruffled Aug 22 '20 at 10:50
  • So a number of additions were needed. Everything except the file name was enclosed in " ", then $, [, ], and / had to be escaped with /. – beruffled Aug 22 '20 at 11:01
  • What is the purpose of s/ at the beginning and // at the end? – beruffled Aug 22 '20 at 11:01
  • It's just `sed` syntax for substitution. You shouldn't have needed to make any changes to my code – Paolo Aug 22 '20 at 11:04