How to replace below text with an empty line in a config file?
# + : STANDANRD\ADMIN_USERS_ONLY : ALL
How to replace below text with an empty line in a config file?
# + : STANDANRD\ADMIN_USERS_ONLY : ALL
sed -i 's/# + : STANDANRD\\ADMIN_USERS_ONLY : ALL//g' config_file
should do the trick. Note the double backslash which is important since a backslash followed by a character has a special meaning to the regex parser. These meta characters are shortcuts for things like a whitespace (\s
), an alphanumeric character (\w
) or in the case of \A
the beginning of a string (the Regular Expression Wikipedia article has a table summarizing the most popular meta characters). Since backslash has this special meaning you have to type a double backslash if you want to match a single backslash in your file.
Example output on CentOS 7:
[tux@centos7 ~]$ cat config_file
# + : STANDANRD\ADMIN_USERS_ONLY : ALL foo
[tux@centos7 ~]$ sed -i 's/# + : STANDANRD\\ADMIN_USERS_ONLY : ALL\(.*\)/\1/g' config_file
[tux@centos7 ~]$ cat config_file
foo
i have a problem here the sed you gave removes entire line even if it has content after
: ALL
your suggestion pls
sed -i 's/# + : STANDANRD\\ADMIN_USERS_ONLY : ALL\(.*\)/\1/g' config_file
. The parantheses need to be quoted here since they have special meaning for Bash.
– Martin Konrad
May 10 '20 at 01:21