How to UNCOMMENT a line that contains a specific string using Sed?

Yes, to comment line containing specific string with sed, simply do:

sed -i '/<pattern>/s/^/#/g' file

And to uncomment it:

sed -i '/<pattern>/s/^#//g' file

In your case:

sed -i '/2001/s/^/#/g' file    (to comment out)
sed -i '/2001/s/^#//g' file    (to uncomment)

Option “g” at the end means global change. If you want to change only a single instance of pattern, just skip this.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)