Since stock AIX sed and awk do not offer in-place editing, you might consider sed
's precursor, ed! It's scriptable and edits files in-place, if you like that sort of thing:
ed -s /tmp/tcpip << 'EOF'
/start \/usr\/lib\/sendmail "$src_running"/s/^/# /
w
q
EOF
This calls ed
on /tmp/tcpip with the -s
option, which suppresses the normal report of the number of bytes read & written. It then sends ed
a quoted here-document containing a list of commands. I made a quoted here-document to prevent any unintentional interpretation of variables, such as $src_running
.
The only interesting ed
command here is the first one; its purpose is to find the line we're after and then comment it out. The last two lines simply w
rite the file back to disk and q
uit ed. The main command exists in two pieces:
- the address, specified by a search range
/start \/usr\/lib\/sendmail "$src_running"/
, and
- the action, which is the search & replacement
s/^/# /
The search range looks a little funny because forward-slashes are the delimiter, so we need escape the forward slashes that are part of the search-text. The search & replacement simply says to replace the beginning of the line (special token ^
) with a hash-mark and a space.
Note that this directly answers your question about finding the (the first) match for that text; it does not replace all of the matching lines. It also doesn't, as written above, care if that line is currently commented out. Commenting out the line twice doesn't hurt anything, but if you want to be stricter with the searching, you could use:
ed -s /tmp/tcpip << 'EOF'
/^[^#]*start \/usr\/lib\/sendmail "$src_running"/s/^/# /
w
q
EOF
The difference here being that we require the seach to be anchored to the beginning of the line (^
) again, and then followed by zero-or-more (*
) characters that are not #
(with [^#]
).