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? Thanks! jlc
On Fri, 2010-03-26 at 00:36 +0000, Joseph L. Casale 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? > > Thanks!---- sounds more like a reason to use sed man sed or tell us exactly what you are trying to do Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
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.... mhr
m.roth at 5-cent.us
2010-Mar-26 13:31 UTC
[CentOS] awk global replacement only after keyword
> 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?{if ( $0 ~ /<keyword>/) (sub(str, repl);} print $0;} mark