Help with replacing a char

Hello All,

I have a file as below . I want to convert the Y with numbers to H

From

4,
M11,
P2521759,
Y75,Y70,Y105,Y110,Y700,Y815,Y830,Y900,Y162,Y300,
Y291,Y290,Y15,Y20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

to

4,
M11,
P2521759,
H75,H70,H105,H110,H700,H815,H830,H900,H162,H300,
H291,H290,H15,H20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

I tried to do

sed -i 's/Y/H/g' file

it converted my Yesterday to Hesterday I want to convert only when there is Y followed by integer

Thanks

As you well know, it always helps to know what operating system and shell you're using when you ask questions in this forum. Assuming that the sed on your system conforms to POSIX Standard requirements (which is not true by default on Linux systems), you could try:

sed 's/Y\([0-9]\)/H\1/g' file

If this doesn't work on your system, please tell us what operating system you're using (including the version number).

1 Like

Hello arunkumar_mca,

Could you please try following too and let me know if this helps you.

awk -F, '{for(i=1;i<=NF;i++){if($i ~ /Y[0-9]+/){sub(/Y/,"H",$i)}}} 1' OFS=,   Input_file

Thanks,
R. Singh

1 Like