[CentOS] awk global replacement only after keyword

Kahlil Hodgson kahlil.hodgson at dealmax.com.au
Fri Mar 26 04:26:28 UTC 2010


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 it then my ugly way. The
> Perl solution would be just as long if it were a one liner, so I'll just shove
> it into a shell script I guess.

i you're worried about slurping the whole file

perl -pe '/# comment/ and $c=1; $c and s/service-one/foo-bar/g' \
      test_file.txt

works for me

slurping the whole files and replacing inline (with backups) you could do

perl -i.save -0777 -pe \
	's/(# comment)(.*)(service-one)/$1$2 foobar/msg' file1 file2 ...

K
-- 
Kahlil (Kal) Hodgson                       GPG: C37B01F4
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1005
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925



More information about the CentOS mailing list