How to sed only that lines that contains given string?

You can prefix most sed commands with an address to limit the lines to which they apply. An address can be a line number, or a regex delimited by /.

cat INPUT | sed '/Select ASDF/ s=sdfg=XXXX='

As mentioned Peter.O, the command as written above will substitute the first occurrence of any sdfg in the string containing Select ASDF. If you need to substitute the exact match to sdfg only in the case it is in fourth column you should go this way:

cat INPUT | sed 's/\(^Select ASDF [^ ]* \)sdfg /\1XXXX /'

If you're only changing column 4 if it has the exact value, then using equality operators instead of regular expressions makes sense.

awk '$1 == "Select" && $2 == "ASDF" && $4 == "sdfg" {$4 = "XXXX"} {print}'

Tags:

Awk

Sed