Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread m . roth
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

Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread m . roth
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. Cool,

Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread Joseph L. Casale
or do you mean blah, blah blah, blah yadda, yadda, keyword, to-be-replaced also-to-be-replaced? Yup, the keyword marks the position where I then start looking for matches. Once I get to work, I will give these a try. Thanks guys! jlc ___ CentOS mailing

Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread m . roth
or do you mean blah, blah blah, blah yadda, yadda, keyword, to-be-replaced also-to-be-replaced? Yup, the keyword marks the position where I then start looking for matches. Once I get to work, I will give these a try. Thanks guys! Sure. And what you want is just { if ($0 ~ /keyword/ ) {

Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread MHR
On Fri, Mar 26, 2010 at 7:28 AM, m.r...@5-cent.us wrote: or do you mean blah, blah blah, blah yadda, yadda, keyword, to-be-replaced also-to-be-replaced? Yup, the keyword marks the position where I then start looking for matches. Once I get to work, I will give these a try. Thanks guys!

Re: [CentOS] awk global replacement only after keyword

2010-03-26 Thread chris procter
or do you mean blah, blah blah, blah yadda, yadda, keyword, to-be-replaced also-to-be-replaced? Yup, the keyword marks the position where I then start looking for matches. Once I get to work, I will give these a try. Thanks guys! Sure. And what you want is just { if ($0 ~

[CentOS] awk global replacement only after keyword

2010-03-25 Thread Joseph L. Casale
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 ___ CentOS mailing list CentOS@centos.org

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Craig White
On Fri, 2010-03-26 at 00:36 +, 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

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Joseph L. Casale
sounds more like a reason to use sed man sed or tell us exactly what you are trying to do Tell you the truth, I would much rather use sed, but I didn't think that was doable with it. I have a slew of txt files that contain a keyword like service-one on many lines. I need to change those, but

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Les Mikesell
Joseph L. Casale wrote: sounds more like a reason to use sed man sed or tell us exactly what you are trying to do Tell you the truth, I would much rather use sed, but I didn't think that was doable with it. I have a slew of txt files that contain a keyword like service-one on many

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Joseph L. Casale
Can you use a regexp like: s/\(known_part\)\(.*\)\(change_part\)/\1\2replace_part/ Unless I misunderstand that, I'd say no. The actual file might look this: /begin file foo bar{ biz service-one baz service-two } --many more of that-- # comment fiz bir{ aaa

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Les Mikesell
Joseph L. Casale wrote: Can you use a regexp like: s/\(known_part\)\(.*\)\(change_part\)/\1\2replace_part/ Unless I misunderstand that, I'd say no. The actual file might look this: /begin file foo bar{ biz service-one baz service-two } --many more of that--

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Joseph L. Casale
I think there is a way to do it in sed using the holding space, but it's so much easier in perl that I never bothered to learn the hard parts. What's the problem with using perl anyway? No problem, just thought there was a sexier way to do it then my ugly way. The Perl solution would be just

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Joseph L. Casale
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. Cool,

Re: [CentOS] awk global replacement only after keyword

2010-03-25 Thread Kahlil Hodgson
On 03/26/2010 02:02 PM, Joseph L. Casale wrote: I think there is a way to do it in sed using the holding space, but it's so much easier in perl that I never bothered to learn the hard parts. What's the problem with using perl anyway? No problem, just thought there was a sexier way to do