For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2006 > Can awk be used to search and replace?









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 Can awk be used to search and replace?
solaris9000

2006-12-01, 7:57 am

Hi

I would like to use awk to search and replace strings in a file. I know
it can be done in sed but would like to try it in awk.

Say I have a file with contents (random data for this example):

ABCDF
123456789
ANNUAL
WEEKLY

And I wish to replace WEEKLY with say MONTHLY. How would this be done
in awk? I am using standard Solaris 9 awk (not nawk or gawk).

Thanks in advance.

Kenny McCormack

2006-12-01, 7:57 am

In article <1164970635.535539.279220@f1g2000cwa.googlegroups.com>,
solaris9000 <solaris9000@hotmail.com> wrote:
>Hi
>
>I would like to use awk to search and replace strings in a file. I know
>it can be done in sed but would like to try it in awk.
>
>Say I have a file with contents (random data for this example):
>
>ABCDF
>123456789
>ANNUAL
>WEEKLY
>
>And I wish to replace WEEKLY with say MONTHLY. How would this be done
>in awk? I am using standard Solaris 9 awk (not nawk or gawk).


1) Don't do that. Using the /usr/bin/awk in Solaris is asking to
stepped on.

2) Use gsub().

Juergen Kahrs

2006-12-01, 7:57 am

Kenny McCormack wrote:

> 1) Don't do that. Using the /usr/bin/awk in Solaris is asking to
> stepped on.
>
> 2) Use gsub().
>


Kenny, you forgot your usual recommendation to use
the XPG AWK.

The OP should use Google to search for "Solaris" or
"Sun" in the archive of this newsgroup. Kenny has
told users so often what to do, this is a FAQ by now.
Kenny McCormack

2006-12-01, 7:57 am

In article <4tao28F134hqgU1@mid.individual.net>,
Juergen Kahrs <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>Kenny McCormack wrote:
>
>
>Kenny, you forgot your usual recommendation to use
>the XPG AWK.


Actually, that's not me. I think some of the other "POSIX thumpers" are
the ones who would recommend using the one in XPG.

I never bother with (i.e., "use" or "recommend") anything other than
GAWK or TAWK.

Janis Papanagnou

2006-12-01, 6:56 pm

solaris9000 wrote:
> Hi
>
> I would like to use awk to search and replace strings in a file. I know
> it can be done in sed but would like to try it in awk.


If you want to use awk on a regular basis you should read a book or
tutorial or a manual. Google for 'awk' and 'tutorial' or 'manual'.

Here's a larger one http://www.gnu.org/software/gawk/manual/gawk.html
though not about (depecated) SUN awk but gawk.

>
> Say I have a file with contents (random data for this example):
>
> ABCDF
> 123456789
> ANNUAL
> WEEKLY
>
> And I wish to replace WEEKLY with say MONTHLY. How would this be done
> in awk? I am using standard Solaris 9 awk (not nawk or gawk).
>
> Thanks in advance.
>


awk 'sub(/WEEKLY/,"MONTHLY")||1'


Janis
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com