I have a configuration file and I want to replace a line containing a specific string, lets say test :
file.conf
aaa
bbb
ccc
// test
ddd
// test
My line of code is not working, I'm new to sed.
sed -i `s#^//[ \t]test#test#g'
Some guidance ?
sed
I have a configuration file and I want to replace a line containing a specific string, lets say test :
file.conf
aaa
bbb
ccc
// test
ddd
// test
My line of code is not working, I'm new to sed.
sed -i `s#^//[ \t]test#test#g'
Some guidance ?
Best Answer
First of all, I'd recommend to avoid the use of
-i
as it will replace the files in-place without confirmation.This is a working version of what you were trying to accomplish and outputs to the standard output (the terminal):
Optionally pipe it through less by appending
| less
to be able to paginate through large amounts of output.The above is all very specific to the sequence of
test
, so to more generalize, you can try this:It will remove the
//
part and any whitespace until the actual text, whatever this text is.If you like the result, you can add the
-i
flag again to replace the file.To tell more about your attempt and explain why my example does work:
'
) should have been used here probably.#
) are your chosen delimiter, a good alternative to the common forward slash (/
) as your match is about this same character and this avoids the need for escaping.The pattern in the command
s/regexp/replacement/
is a standard one as mentioned in the manpage, among many others:man 1 sed
:rexexp
in the above is a commonly used abbreviation for regular expressions.^//[ \t]test$
matches lines starting (^
) with two forward slashes, followed by a space or a tab character, followed by the exact sequencetest
and a line ending ($
).g
flag is to do this operation in a global way as explained here.