Re: awk global replacement only after keyword

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Thu, Mar 25, 2010 at 5:36 PM, Joseph L. Casale
<jcasale@xxxxxxxxxxxxxxxxx> wrote:
> Trying to avoid a perl script which wouldn't be hard, but I am looking
> for an awk one liner that does a replacement, but only after it sees a
> key word on some line.
>
> Anyone know of that's easy to do?
>

Depends on how you define "one-liner."  Something like this might work:

{ if index($0, PATTERN) != 0 {FOUND = 1;}; if (FOUND != 0)
{subst(REPLACE_THIS, WITH_THIS, $0); }

You'd want to reverse the order of the two statements if the
replacement is only to occur after the pattern is found.

Now, if you want to process multiple files, you'd have to reset the
found flag when the filename changes, and that's another if-else
clause, which gets kind of long for a "one liner."

It's ugly, but it's awk....

mhr
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux