Search code examples
sedaix

Find a line containing a UNIX path and comment it from a file


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}"

Solution

  • 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}"