2

I would like the line containing 'start /usr/lib/sendmail "$src_running"' commented in a file

$ grep /usr/lib/sendmail /tmp/tcpip
# "/usr/lib/sendmail -bi" or "/usr/ucb/newaliases".
start /usr/lib/sendmail "$src_running" "-bd -q${qpi}"


$ grep /usr/lib/sendmail /tmp/tcpip
# "/usr/lib/sendmail -bi" or "/usr/ucb/newaliases".
#start /usr/lib/sendmail "$src_running" "-bd -q${qpi}"
rony thomas
  • 215
  • 1
  • 12
  • `sed` command is commonly used for this. There are many answers across the SE network addressing this, maybe one of them can help you. https://stackexchange.com/search?q=comment+out+line+sed – virullius Mar 25 '19 at 17:32
  • Thank you, I am still searching/trying to figure out and I know it's a bit hard to get this sort of command to work on AIX – rony thomas Mar 25 '19 at 18:13

3 Answers3

1

sed cannot search for strings, only regexps (see Is it possible to escape regex metacharacters reliably with sed), so you're better off using a tool like awk that does understand strings when you want to match on a string:

$ awk -v str='start /usr/lib/sendmail "$src_running"' 'index($0,str){$0="#"$0} 1' file
# "/usr/lib/sendmail -bi" or "/usr/ucb/newaliases".
#start /usr/lib/sendmail "$src_running" "-bd -q${qpi}"
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

The sed command is suitable here as well. Here is a sample solution:

sed -r 's|start /usr/lib/sendmail "\$src_running"|#&|' inputfile

An explanation:

-r option provide extended Regular Expression avoiding extensive quoting. But is Linux specific and less portable across other sed versions.

s sed command for substitution.

| search pattern marker, and replacement pattern marker

\$ quoted $, escaping $ regexp end of line anchor

#& replacement pattern, # is the comment prefix, & is the matched search pattern

Hope this works for you.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
0

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 write the file back to disk and quit 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 [^#]).

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38