| Ed Morton 2005-02-08, 8:48 pm |
|
rajashekar14@yahoo.com wrote:
> Can anyone let me know the command for finding andreplacing nth word in
> mth line using some unix command.
awk 'NR==m{$n="replacement"}{print}' file
> I will explain it with a example.
>
> The File temfile reads looks like this,
>
> Line1 -- This is a TEST to be conucdted
> Line2 -- Please have a solution to it
> Line3 -- what is this all AROUND with
>
> Q1) Now I want to replace 4rd word of 1st Line(word is "TEST")is to be
> replaced with word "Compt". How to do this ?
awk 'NR==1{$4="Compt"}{print}' file
> Q2)Now I want to replace 5rd word of 3rd Line(word is "AROUND")is to be
> replaced with word "RING". How to do this.
awk 'NR==3{$5="RING"}{print}' file
> which command is to used to do so?.
>
To do it all at one time:
awk 'NR==1{$4="Compt"}
NR==3{$5="RING"}
{print}' file
Use a tmp file if you want to put the result back into the original file.
Regards,
Ed.
|