Replace string in column

Hi,

I want to replace string in column,Example i have file caleed a1.txt ,want to replace string "A12" with "A23" only in column2 ,not from file itself.Using sed command replace string in file itself.

Thanks,
Mohan

Try...

awk '{sub("A12","A23",$2);print}' a1.txt

cat al.txt| cut -f2 |sed -e 's/A12/A23/g' .......... this may work i m not sure.

1 Like

This wont work if you have multiple entries of A12 in some other column other than 2nd column

$ cat sampfile
1 A12 A12
2 A99 20
3 A34 30
4 A45 40
5 A12 50
 
$ cat sampfile| cut -f2 |sed -e 's/A12/A23/g'
1 A23 A23
2 A99 20
3 A34 30
4 A45 40
5 A23 50

And moreover if you are specifying a field dont forget to specify the delimiter as the default delimiter for cut is tab and not space...

As you can see from below...

$ cat sampfile
1 A12 A12
2 A23 20
3 A34 30
4 A45 40
5 A12 50
 
$ cat sampfile| cut -f2
1 A12 A12
2 A23 20
3 A34 30
4 A45 40
5 A12 50

No difference b/w 1st and 2nd command

$ cat sampfile| cut -d " " -f2
A12
A23
A34
A45
A12

The above makes some sense but he doesnt want to just print one column and make changes... he needs to make changes in entire file...