Srinivas Nallapati Srinivas Nallapati - 2 months ago 7
Linux Question

Replace very near word to a number with another word

Replace very near string to a number with another string

a b c d a b c d e 23:12:00 k b c l d b d c e 12:02:10


How to replace first d before time (number) with another character f.it should be as below

a b c d a b c f e 23:12:00 k b c l d b f c e 12:02:10


OR ,

How to Replace cell with header .

[srinivas@examplewww ~]$ grep -oP '(cell">)[0-2][0-9]+:[0-9]+:[0-9]+' test.html1
cell">06:25:50
cell">23:59:38
cell">23:17:50
cell">06:27:37
cell">06:07:38


it should be as below

header ">06:25:50
header ">23:59:38
header ">23:17:50
header ">06:27:37
header ">06:07:38

Answer

This might work for you (GNU sed):

sed -r 's/d([^d:]*[0-2][0-3].:..:..)/f\1/g' file

and

sed -r 's/cell(">[0-2][0-3]:..:..)/   header \1/' file

Both regexp pattern match and then use backreferences in the replacement side of the substitution.

Comments