in reply to replace all but first

Firstly, there isn't much information you've given us. The layout of the file may be helpful. Secondly, I would reccomend using tr rather than s (see perlop). Since you are not replacing, but rather transliterating. However, if you wanted to replace a with ABBA (haha) then you would want to use the s operator.


Grygonos

Replies are listed 'Best First'.
Re^2: replace all but first
by texuser74 (Monk) on Sep 03, 2004 at 13:52 UTC
    This is what i am trying to do

    my text file has
    <cnt>Germany</cnt><cmp>ALLIANZ INSURANCE</cmp>
    <cnt>Germany</cnt><cmp>ALLIANZ PARKWAY</cmp>
    <cnt>Germany</cnt><cmp>ALLIED DOMECQ</cmp>

    i want to replace it as
    <cnt>Germany</cnt><cmp>ALLIANZ INSURANCE</cmp>
    <TAB><cmp>ALLIANZ PARKWAY</cmp>
    <TAB><cmp>ALLIED DOMECQ</cmp>

    i.e. i want to replace "<cnt>Germany</cnt>" to "<TAB>" from its second occurance

      use strict; use warnings; my %seen; while ( <DATA> ) { next unless /^(<cnt>([^<]+)<\/cnt>)(.*)$/; print $seen{$2}++ ? "<TAB>$3\n" : "$1$3\n"; } __DATA__ <cnt>France</cnt><cmp>ACCOR</cmp> <cnt>France</cnt><cmp>AGF</cmp> <cnt>France</cnt><cmp>AIR LIQUIDE</cmp> <cnt>Germany</cnt><cmp>ALLIANZ INSURANCE</cmp> <cnt>Germany</cnt><cmp>ALLIANZ PARKWAY</cmp> <cnt>Germany</cnt><cmp>ALLIED DOMECQ</cmp> <cnt>United Kingdom</cnt><cmp>LEICESTER CITY FOOTBALL CLUB</cmp> <cnt>United Kingdom</cnt><cmp>SPENCER GEARS</cmp>
      If it's not going to appear twice on the same line:
      my $seen=0; while(<DATA>) { if ($seen) { s{<cnt>Germany</cnt>}{<TAB>} } else { $seen ||= m{<cnt>Germany</cnt>} } print; } __DATA__ <cnt>Germany</cnt><cmp>ALLIANZ INSURANCE</cmp> <cnt>Germany</cnt><cmp>ALLIANZ PARKWAY</cmp> <cnt>Germany</cnt><cmp>ALLIED DOMECQ</cmp>

      Caution: Contents may have been coded under pressure.
      $data =~ s{(<cnt>Germany</cnt>)}{$seen++?'<TAB>':$1}ge;