I need to comment out an entry in rc.tcpip file inside /etc

# Start up the snmpmibd daemon
start /usr/sbin/snmpmibd "$src_running"

# Start up the Simple Network Management Protocol (SNMP) daemon
start /usr/sbin/snmpd "$src_running"

How do I comment these lines using sed?

  • 1
    What makes those lines unique? We need to know something that only those lines contain. Would snmp be unique for example?
    – terdon
    May 9, 2014 at 13:59

3 Answers 3


To comment out lines that start with start /usr/sbin/snmpmibd, use the s command with a pattern using the ^ anchor and # in the replacement text, plus & which stands for the replaced text. You can either match the two lines separately, or notice that snmpd is snmpmibd with mib omitted (concision at the expense of clarity)¹ and use snmp\(mib\)\{0,1\}d. Since the pattern contains a slash, use another character as the delimiter, such as !. Since sed is a filter, you'll need to write the output to a new file, then move the new file into place (redirecting the output of sed to the same file as the input wouldn't work: it would first erase the old file, then start reading from the now-empty file)².

sed -e 's!^start /usr/sbin/snmpmibd !#&!' -e 's!^start /usr/sbin/snmpmibd !#&!' </etc/rc.tcpip >/etc/rc.tcpip.new
mv /etc/rc.tcpip.new /etc/rc.tcpip

To edit a file in place, you can use ed instead of sed.

ed -s /etc/rc.tcpip <<'EOF'
g!^start /usr/sbin/snmpd ! s/^/#/
g!^start /usr/sbin/snmpmibd ! s/^/#/

¹ AIX doesn't have \? or \| operators, only started BRE, so \(snmpmibd\|snmpd\) won't work.
² AIX sed doesn't have -i, that's a GNU extension.

  • not necessary to add the 2 section of action in sed, just separe this with a ; and -e is not mandatory in case of AIX sed :-) May 12, 2014 at 6:29
  • My thanks to you both @Gilles and @NeronLeVelu!
    – batil
    May 13, 2014 at 0:54

Thanks to Gilles for the remark on AIX sed not allowing -i

to comment lines

sed 's/^[[:space:]]*[^#]/#&/' /etc/rc.tcpip > /tmp/rc.tcpip
cat /tmp/rc.tcpip /etc/rc.tcpip
rm /tmp/rc.tcpip 

a filter on line to select could be done adding /yourRegExFilterPattern/ before the s like :

sed '/start/ s/^[[:space:]]*[^#]/#&/' /etc/rc.tcpip > /tmp/rc.tcpip
cat /tmp/rc.tcpip /etc/rc.tcpip
rm /tmp/rc.tcpip 

or, more efficient but less readable on complex filter (not the case here with a simple start)

sed 's/^[[:space:]]*start/#&/' /etc/rc.tcpip > /tmp/rc.tcpip
cat /tmp/rc.tcpip /etc/rc.tcpip
rm /tmp/rc.tcpip 

to uncomment lines (need the filter directly pattern to avoid uncommenting real comments)

sed '/YourSelectionPattern/ s/^\(^[[:space:]]*\)#/\1/' /etc/rc.tcpip > /tmp/rc.tcpip
cat /tmp/rc.tcpip /etc/rc.tcpip
rm /tmp/rc.tcpip 

You may need extra rights to modify this file if you are not root, so a sudo is the key.

I also recommend making a backup copy of /etc/rc.tcpip before just in case.

  • 1
    sed -i is a GNU extension, not present on AIX. May 10, 2014 at 11:34
  • @Gilles: sorry I forget the AIX from the title and no reference in request (+1 on your remark). Make the correction in my post May 12, 2014 at 6:22

In /etc/rc.tcpip, comment out the snmpd entry by running command:

chrctcp -d snmpd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .