Pattern matching and inserting a line in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pramodkh
    New Member
    • Nov 2007
    • 23

    Pattern matching and inserting a line in a file

    Hi All

    I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line.

    for example,
    I have the following content in a file:

    //This is my source file
    //this is where i want to insert a line
    Code:
    class Class1
    {
        data members
        member functions
    }
    
    //this is where i want to insert a line
    class Class2
    {
        data members
        member functions
    }
    I have to match for a pattern "Class" and insert a line before that pattern match.

    This is what i have tried so far:

    [CODE=perl]$presentFlag = 0;
    $notPresentFlag = 0;

    ##$ Open source file for reading the contents
    open(SOURCE,"<" ,$sourceFile) or die( "File not found...$!");
    ### open new file for inserting new line -> [TestFixture], after matching a pattern
    open(DEST,">",$ destFilename) or die("File not found...$!");

    while($fileLine = <SOURCE>){
    if( $fileLine =~ m/public class/ ){ # pattern to be matched in source file
    if( $previousLine =~/[TestFixture]/ ){ # check for previous line if it already has new text
    print "\n Pattern already present in FN $destFilename.. .$previousLine" ;
    $presentFlag = 1;
    }
    else {
    print "\n Pattern NOT present in FN $destFilename.. .$previousLine" ;
    $notPresentFlag = 1;
    }
    }
    ##### If pattern already present then print the file contents as it is in dest file
    if($presentFlag ){

    print DEST $fileLine;
    }
    ## Get the previous line here
    $previousLine=$ fileLine;
    }# end of while

    ### If pattern matches and doesnt have the previous line as required
    if($notPresentF lag){
    ### I am stuck here....how do i move my insertion pointer to previous line in my source and dest file???
    ### do i need to have a counter or is there any other alternate way?
    }
    close(SOURCE);
    close(DEST);[/CODE]


    Thanks in advance.

    Regards
    Pramod
    Last edited by eWish; Feb 19 '08, 03:14 PM. Reason: Please use [CODE][/CODE] tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    From your description, I feel this is what you need:
    [CODE=perl]

    $i=0;
    $test_fixture=" xxxxxxxxxxxxx"; ###put your [Test Fixture]
    open(SOURCE,"<" ,$sourceFile) or die( "File not found...$!");
    open(DEST,">",$ destFilename) or die("File not found...$!");
    @source=<SOURCE >;
    while(@source)
    {
    if( /public class/ ) { ##search for the pattern in each line
    ### insert test fixture if previous line doesn't have it already
    print DEST "$test_fixture\ n" unless($source[$i-1]=~/$test_fixture/);
    print DEST $_;
    }
    else {
    print DEST $_;
    }
    $i++;
    }

    close(SOURCE);
    close(DEST);

    [/CODE]

    Comment

    • npidaparthy
      New Member
      • Feb 2008
      • 14

      #3
      Hi

      read the file contents into an array like this
      [CODE=perl]@array = <SOURCE>;[/CODE]

      once the file is in the array you can loop through the array and take two lines [none od them should ne null - betwwn two lines there might be blank lines, they have to be supressed]
      then
      [CODE=perl]if( $arr[i]=~ m/public class/ ) and $arr[i-1] !~ $lineToBeInsert ed
      #tthen
      print DEST $arr[i-1]
      print DEST $lineToBeInsert ed[/CODE]

      try this in this manner
      Last edited by eWish; Feb 19 '08, 03:17 PM. Reason: Please use [CODE][/CODE] tags

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        It would help greatly if you showed the real text file you are trying to insert a line into. This part of your code makes little sense out of context and looks like it will just be wrong since you are using square brackets improperly /[TestFixture]/:

        Code:
           if( $fileLine =~ m/public class/ ){ # pattern to be matched in source file
               if( $previousLine =~/[TestFixture]/ ){ # check for previous line if it already has new text
        Inserting lines into files is easily done using perls inplace editor once you learn how to get it running, which is extremely simple but few perl books or tutorials cover the use of the inplace editor. It is also extrememly fast so editing even big files should be quick.

        Comment

        • pramodkh
          New Member
          • Nov 2007
          • 23

          #5
          Thanks a lot Nithin. and thanks Npidaparthy for optimized code.
          I am able to achieve to some extent of what was required.
          But the complexity is incresing now. I will get back in case i need any further help on this.
          Kevin, please send me more information on PERL inplace editor for Windows platform. i have seen that some of the statements work only in Unix( for ex: perl -p -i.bak -e.....). Thanks in advance.

          Thanks everyone.

          Regards
          Pramod

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            the inplace editor for Windows works the same as Unix. The only differences I am aware of is you have to define a backup extension with Windows (-i.bak) and you use double-quotes instead of single-quotes around the code part:

            -e "some code here"

            Comment

            Working...