This is more suitable for awk
or any other tool that understands columns:
awk '{if ($9=="ice") {$11="keep"} print}' inputfile
Fields in awk are delimited by space by default. $9
would denote the 9th field. If the 9th field is ice
, change the 11th to keep
.
For your input, it'd produce:
a b c d e f g h ice j keep l m
You could do it using sed
too, but it's not quite straight-forward:
sed -r 's/^(([^ ]+ ){8}ice \S+ )(\S+)/\1keep/' inputfile
Here ([^ ]+ ){8}
would match the first 8 fields. (([^ ]+ ){8}ice \S+ )
matches 10 fields with the 9th being ice
and captures it into a group that is substituted later making use of a backreference, \1
.