Unix Shell - Re: How to Search and replace nth word in mth line using some unix

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > Re: How to Search and replace nth word in mth line using some unix





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: How to Search and replace nth word in mth line using some unix
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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com