简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM