[CentOS] awk global replacement only after keyword

Fri Mar 26 23:53:53 UTC 2010
chris procter <chris-procter at talk21.com>

> >>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/ ) 
> {
>     start = 1;
>  }
>  if ( start == 1 ) 
> {
>     sub( str, repl );
>     print $0;
>  
> }
>}


If you make this a little more awky then you get the one liner:-

awk '(start==1){gsub(/str/,"repl")}/keyword/{start=1}{print $0}' input_file.txt

where keyword is the trigger phrase, str is a regexp to find, repl is the string to replace the regexp with, and input_file.txt is obviously your data file (I'm assuming you want to print all the lines not just the replaced ones)

If you only want to replace matches on lines after the one with keyword then refining it slightly to:

awk '(start==1){gsub(/str/,"repl");continue}/keyword/{start=1}{print $0}' input_file.txt

should work.


chris