Pattern matching and subsitution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    Pattern matching and subsitution

    Hi all,

    I have a file say test.txt which has the following lines

    @TS_FILE:|: win32version\fi les\TEST_5643_P CB53105
    @TS_FILE:|: win32version\fi les\TEST_5645_P CA53105

    I need to replace the above lines as below in the same file
    @TS_FILE:|: win32version\fi les\PCB53105
    @TS_FILE:|: win32version\fi les\PCA53105

    I need to remove "TEST_XXXX_ " form it..

    I tried the same but Iam not able to even match the required line Can anyone tell me how
    to do the same..iam new to perl..

    [CODE=perl]
    open (FH1, "TestPlan.t st") || die "Can't find the tst file";
    my $var='@TS_FILE: |: win32version\At tached_files\TE ST_';
    print $var;
    while (<FH1>) {
    $temp = $_;
    if ($temp =~ /"$var"/) {
    print $_;
    print "Match Found";
    }
    }
    close(FH1);
    [/CODE]
    Last edited by miller; May 23 '07, 04:28 PM. Reason: Code Tag and ReFormatting
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    perl -i -p -e's/TEST_\d*_//g' TestPlan.tst

    Google "perl edit in place" for more information.

    HTH,
    Paul

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      why did you add the double-quotes into the regexp?

      if ($temp =~ /"$var"/) {




      Code:
      #open (FH1, "TestPlan.tst") || die "Can't find the tst file";
      my $var='@TS_FILE:|: win32version\Attached_files\TEST_';
      print $var,"\n\n";
      while (<DATA>) {
         $temp = $_;
         if ($temp =~ /$var/) {
            print "Match Found -> $_";
         }
      }
      #close(FH1);
      __DATA__
      @TS_FILE:|: win32version\files\TEST_5643_PCB53105
      @TS_FILE:|: win32version\files\TEST_5645_PCA53105

      Comment

      Working...