linux - How to comment out only uncommented lines with Sed and leave blank/empty lines 100% intact? -


i’m putting bash configuration scripts configure linux server , able comment uncommented lines in file, leave blank/empty lines intact , untouched.

using the great answer question able come sed 1 liner works nicely:

sed -e '/^#/!s/\(.*\)/# \1/g' /etc/update-motd.d/90-updates-available 

of course when run sed command on production setup -e swapped -i write file in place, anyway contents of file commented out shown below:

#!/bin/sh #  # if [ -x /usr/lib/update-notifier/update-motd-updates-available ]; #     exec /usr/lib/update-notifier/update-motd-updates-available # fi 

but ideally have output this; note how 1 blank/empty line untouched.:

#!/bin/sh  # if [ -x /usr/lib/update-notifier/update-motd-updates-available ]; #     exec /usr/lib/update-notifier/update-motd-updates-available # fi 

is there way in sed?

how this:

sed -e 's/^\([^#].*\)/# \1/g' /etc/update-motd.d/90-updates-available 

Comments