[Solved] Replace first occurrence of a pattern if not preceded with another pattern


This might work for you (GNU sed):

sed '/\<cat\>.*\<bird\>/b;s/\<\(bird\) \+[0-9]\+/\1 0/;T;:a;n;ba' file

If a line contains the word cat before the word bird end processing for that line.
Try to substitute the number following the word bird by zero. If not successful end processing for that line. Otherwise read/print all following lines until the end of the file.

Might also be written:

sed -E '/cat.*bird/b;/(bird +)[0-9]+/{s//\10/;:a;n;ba}' file

2

solved Replace first occurrence of a pattern if not preceded with another pattern