sed '0,/a/s/a/b/' demo replace first a to b 0,/a is the range specifier begin from 0 line to first occurance to a #!/bin/bash for i in `seq 0 30` do echo $i sed -i "1,/func()/s/func()/func$i()/" func.c done