Phillip H. Blanton
Phillip H. Blanton

Reputation: 525

I need to use sed to comment out two lines in a text file

I am running a custom kernel build and have created a custom config file in a bash script, now I need to comment out two lines in Kbuild in order to prevent the bc compiler from running. The lines are...

$(obj)/$(timeconst-file): kernel/time/timeconst.bc FORCE
    $(call filechk,gentimeconst)

Using Expresso, I have a regex that matches the first line...

^\$\(obj\)\/\$\(timeconst-file\): kernel\/time\/timeconst\.bc FORCE

Regex Match

But can't get sed to actually insert a # in front of the line.

Any help would be much appreciated.

Upvotes: 0

Views: 989

Answers (2)

TemporalWolf
TemporalWolf

Reputation: 7952

sed -i "/<Something that matches the lines to be replaced>/s/^#*/#/g"

This uses a regex to select lines you want to comment/<something>/, then substitutes /s/ the start of the string ^(plus any #*s already there, with #. So you can comment lines that are already commented no problem. the /g means continue after you found your first match, so you can do mass commenting.

I have a bash script that I can mass comment using the above as:

sed -i.bkp "/$1/s/^#\+\s*//g" $2 i.bkp makes a backup of the file named .bkp

Script is called ./comment.sh <match> <filename>

The match does not have to match the entire line, just enough to make it only hit lines you want.

Upvotes: 1

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27516

You can use following sed for replacement:

sed  's,^\($(obj)/$(timeconst-file): kernel/time/timeconst.bc FORCE\),#\1,'

You don't need to escape ( ) or $, as in sed without -r it is treated as literal, for grouping \( \) is used.

Upvotes: 0

Related Questions