Comment out lines matching pattern with Sed

Let’s say you’ve got a file with the following lines:

cat file.txt 
leave me alone
leave me alone
comment me out
leave me alone
comment me out also
leave me alone

Comment out the lines that are begging for it:

sed -i '/comment me out/s/^/#/g' file.txt

Voila:

cat file.txt 
leave me alone
leave me alone
#comment me out
leave me alone
#comment me out also
leave me alone

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word