Home > Archive > AWK > June 2004 > How to replace field in a line
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 |
How to replace field in a line
|
|
|
| I apologize if this is not the correct forum, but I am trying to
accomplish the following and think awk is the best method to
accomplish this:
Sample Lines from a Text file:
-------------------------------
machine-1 role-A site-A startup-method1
machine-2 role-A site-A startup-method1
machine-3 role-B site-A startup-method2
machine-4 role-C site-B startup-method3
machine-5 role-C site-B startup-method3
I want to be able to replace the site to "site-B" for all those lines
with a role of "role-A". I could use PERL and do every line, but
there has to be a quicker/more efficient way to do this with a
combination of grep/sed/awk. So the finished file would look like:
Finished file:
-------------------------------
machine-1 role-A site-B startup-method1
machine-2 role-A site-B startup-method1
machine-3 role-B site-A startup-method2
machine-4 role-C site-B startup-method3
machine-5 role-C site-B startup-method3
Thanks for your help,
Sean
| |
| Chris F.A. Johnson 2004-06-07, 8:55 pm |
| On 2004-06-07, Sean wrote:
> I apologize if this is not the correct forum,
It's quite acceptable here, but comp.unix.shell might have been
more appropriate.
> but I am trying to
> accomplish the following and think awk is the best method to
> accomplish this:
>
> Sample Lines from a Text file:
> -------------------------------
> machine-1 role-A site-A startup-method1
> machine-2 role-A site-A startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
>
> I want to be able to replace the site to "site-B" for all those lines
> with a role of "role-A". I could use PERL and do every line, but
> there has to be a quicker/more efficient way to do this with a
> combination of grep/sed/awk. So the finished file would look like:
>
> Finished file:
> -------------------------------
> machine-1 role-A site-B startup-method1
> machine-2 role-A site-B startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
sed '/role-A/ s/site-A/site-B/' FILE
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Ed Morton 2004-06-07, 8:55 pm |
|
Chris F.A. Johnson wrote:
> On 2004-06-07, Sean wrote:
<snip>
<snip>[color=darkred]
>
>
> sed '/role-A/ s/site-A/site-B/' FILE
>
That would fail if role-A or site-A can appear anywhere else on the
line, i.e. as part of one of the other fields. For example if the real
value for "machine-1" is "Client/Server-1" and the real value for
"role-A" is "Client".
This will work:
gawk '$2 == "role-A"{$3 = "site-B"} {print}' file
Regards,
Ed.
| |
| Chris F.A. Johnson 2004-06-07, 8:55 pm |
| On 2004-06-07, Ed Morton wrote:
>
> Chris F.A. Johnson wrote:
><snip>
>
><snip>
>
> That would fail if role-A or site-A can appear anywhere else on the
> line, i.e. as part of one of the other fields. For example if the real
> value for "machine-1" is "Client/Server-1" and the real value for
> "role-A" is "Client".
>
> This will work:
>
> gawk '$2 == "role-A"{$3 = "site-B"} {print}' file
But will not maintain the formatting:
machine-1 role-A site-B startup-method1
machine-2 role-A site-B startup-method1
machine-3 role-B site-A startup-method2
machine-4 role-C site-B startup-method3
machine-5 role-C site-B startup-method3
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
| |
| Stefan Lagotzki 2004-06-08, 3:55 pm |
| Sean <aether8203@yahoo.com>:
> I apologize if this is not the correct forum, but I am trying to
> accomplish the following and think awk is the best method to
> accomplish this:
>
> Sample Lines from a Text file:
> -------------------------------
> machine-1 role-A site-A startup-method1
> machine-2 role-A site-A startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
>
>
> I want to be able to replace the site to "site-B" for all those lines
> with a role of "role-A". I could use PERL and do every line, but
> there has to be a quicker/more efficient way to do this with a
> combination of grep/sed/awk. So the finished file would look like:
>
> Finished file:
> -------------------------------
> machine-1 role-A site-B startup-method1
> machine-2 role-A site-B startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
awk '$2=="role-A" {
part1=substr($0,1,index($0,$3)-1);
part2=substr($0,index($0,$3),index($0,$4
)-index($0,$3));
gsub("site-A","site-B",part2);
part3=substr($0,index($0,$4));
$0=sprintf("%s%s%s",part1,part2,part3);
}
{print}' << EOF
machine-1 role-A site-A startup-method1
machine-2 role-A site-A startup-method1
machine-3 role-B site-A startup-method2
machine-4 role-C site-B startup-method3
machine-5 role-C site-B startup-method3
EOF
The result:
machine-1 role-A site-B startup-method1
machine-2 role-A site-B startup-method1
machine-3 role-B site-A startup-method2
machine-4 role-C site-B startup-method3
machine-5 role-C site-B startup-method3
HTH,
Stefan
..
| |
|
| Thanks for all your help all! One last question, how would you
replace anything in column 3 with the text of your choice. Many of
the options presented here replace "site-A" with "site-B" (which is
exaclty what I asked - thank you), but what if column 3 was "moocow"
or "foobar"? How would you say "replace column 3 with xxxxx" ?
Thanks,
Sean
Stefan Lagotzki <lago20@gmx.de> wrote in message news:<ca4d63$lui$05$1@news.t-online.com>...
> Sean <aether8203@yahoo.com>:
>
> awk '$2=="role-A" {
> part1=substr($0,1,index($0,$3)-1);
> part2=substr($0,index($0,$3),index($0,$4
)-index($0,$3));
> gsub("site-A","site-B",part2);
> part3=substr($0,index($0,$4));
> $0=sprintf("%s%s%s",part1,part2,part3);
> }
> {print}' << EOF
> machine-1 role-A site-A startup-method1
> machine-2 role-A site-A startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
> EOF
>
> The result:
>
> machine-1 role-A site-B startup-method1
> machine-2 role-A site-B startup-method1
> machine-3 role-B site-A startup-method2
> machine-4 role-C site-B startup-method3
> machine-5 role-C site-B startup-method3
>
>
> HTH,
> Stefan
>
> .
| |
| Ed Morton 2004-06-09, 3:55 am |
|
Sean wrote:
> Thanks for all your help all! One last question, how would you
> replace anything in column 3 with the text of your choice. Many of
> the options presented here replace "site-A" with "site-B" (which is
> exaclty what I asked - thank you), but what if column 3 was "moocow"
> or "foobar"? How would you say "replace column 3 with xxxxx" ?
See the solution I posted earlier.
Ed.
|
|
|
|
|