[CentOS] awk global replacement only after keyword

MHR mhullrich at gmail.com
Fri Mar 26 04:12:46 UTC 2010

On Thu, Mar 25, 2010 at 5:36 PM, Joseph L. Casale
<jcasale at activenetwerx.com> 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....


More information about the CentOS mailing list