With sed, how can I replace word within a matching line

regular expressionsed

I am trying to write a sed expression to detect lines similar to this:

s0:12345:respawn:/sbin/agetty -8 -s 115200 ttyS0 linux

and replace the ttys0 with something_else

I do not want to detect commented out lines (beginning with #)

I can replace the whole line, but how to I just replace the substring, leaving the rest of the line intact ?

Best Answer

One way to do this is as follows:

echo 's0:12345:respawn:/sbin/agetty -8 -s 115200 ttyS0 linux' | 
    sed -e '/^[^#]/ s/ttyS0/foo/'
Related Question