Xref: utzoo comp.unix.shell:2021 comp.lang.perl:5074 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!stanford.edu!agate!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal L. Schwartz) Newsgroups: comp.unix.shell,comp.lang.perl Subject: Re: search and replace string from a script Keywords: search replace script Message-ID: <1991Apr25.171013.14963@iwarp.intel.com> Date: 25 Apr 91 17:10:13 GMT References: <1991Apr23.180034.7349@progress.com> <1991Apr23.215804.8366@iwarp.intel.com> <1991Apr25.040751.1991@ttsi.lonestar.org> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal L. Schwartz) Followup-To: comp.unix.shell Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 33 In-Reply-To: root@ttsi.lonestar.org (System) In article <1991Apr25.040751.1991@ttsi.lonestar.org>, root@ttsi (System) writes: | >perl -pe '/STRING1/ && (++$n == 20) && s/STRING1/STRING2/' out | > | >perl -pe 'if (/STRING1/ && (++$n == 20)) { s/STRING1/STRING2/; }' out | > | >perl -pe 'if (/STRING1/) { s/STRING1/STRING2/ if ++$n == 20; }' out | > | >perl -pe 's/STRING1/STRING2/ if /STRING1/ && (++$n == 20);' out | > | >All of these presume "20" is your magic occurance. Season to taste. | > | >print "Just another Perl hacker," # Perl is available from all GNU sites... | | These all presume only one occurrence or first occurrence per line. | Is that the requirement? How would you handle counting multiple | occurrences per line in perl? Thanks for posting the solutions above. OK, now you did it. You asked me to get ugly. Hang on to your hat... perl -pe 's/STRING1/(++$n == 20) ? "STRING2" : $&/ge' out You might need to look at this one for a while to figure out what's happening. Trust me, it'll be good for you. :-) (And if you can't figure it out, I know this great book on the subject... :-) print "Just another Perl hacker,"; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/