Match pattern and Extract section from file

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

    Match pattern and Extract section from file

    Hi All
    Today only i joined this group. need ur help.
    Here goes my question:

    Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched section?

    I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions.

    for ex:

    #ifdef X
    some code
    #endif

    #ifdef Y
    somecode
    #ifdef Z
    somemorecode
    #else
    code that does not match condition
    #endif
    #endif

    ---------------------------------
    from the above text, i have to extract sections between #ifdef and #endif.( and ofcourse #else in some cases)

    let me know for any further explanation about the same.

    thanks in adv
    Pramod
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by pramodkh
    Hi All
    Today only i joined this group. need ur help.
    Here goes my question:

    Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched section?

    I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions.

    for ex:

    #ifdef X
    some code
    #endif

    #ifdef Y
    somecode
    #ifdef Z
    somemorecode
    #else
    code that does not match condition
    #endif
    #endif

    ---------------------------------
    from the above text, i have to extract sections between #ifdef and #endif.( and ofcourse #else in some cases)

    let me know for any further explanation about the same.

    thanks in adv
    Pramod
    Well, considering this is a learning forum and not a script writing service, I am obligated to ask you what you have tried thus far.

    Please post your code that you have tried, in the necessary code tags, and we will help guide you from there.

    Regards,

    Jeff

    Comment

    • eggi
      New Member
      • Nov 2007
      • 9

      #3
      To get you started, you can extract elements from a perl match using parentheses on the left hand side and $ variables on the right

      for instance

      [code=perl]
      $string="what is it"
      print "$string\n" ;
      $string =~ /^.* (is) .*$/$1/;
      print "$string\n" ;
      [/code]

      first print should get you "what is it"
      second print should just be "is"

      Hope this helps get you started!

      , Mike
      Last edited by numberwhun; Nov 25 '07, 03:04 PM. Reason: add code tags

      Comment

      • pramodkh
        New Member
        • Nov 2007
        • 23

        #4
        Here is what i hav tried so far.

        1. Get the C file contents into array
        2. Read elements of array(i.e lines of file)
        3. check if it matches with '#ifdef and #endif'
        4. set the flags( ifdefflag and endifflag)
        5. take actions accordingly ( i mean write contents of ifdef into a temp file excluding #ifedf and #endif)

        This is becoming complex to include all conditions like ( to check for #else and nested #ifdef )

        Let me know is there any simple solution to achieve this. flag setting doesnt seem to be so useful in some cases.

        Thanks
        Pramod

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          The pseudo code that you supplied is not your actual code. Please provide your code, surrounded by the necessary code tags, and we will try and help you.

          Regards,

          Jeff

          Comment

          • pramodkh
            New Member
            • Nov 2007
            • 23

            #6
            Hi,

            Here is the code...need to format and change some of the declarations.
            let me know if you need anu further comments to understand the code.

            [code=perl]
            # Global variable Declaration and Initialization
            my $ifdefflag = 0;
            my $elseflag = 0;
            my $endifflag = 0;
            my $counter = 0;
            my $flag=0;
            my $ctrToLookForif def = 0;
            my @filecontents;
            my @splititem;
            my @compiler_switc hes =(A,B,C); #has list of compiler switches used with #ifdef

            #### Open File for reading the contents
            open FH, "source.c" or die "can not open source.c...$!";
            while (<FH>) {
            @filecontents = <FH>;
            }
            #### Backing up source file using system command
            `copy source.c source.bak`;
            #### Create new file after refactoring
            open NEWFH, ">source.c" or die "can not open source.c...$!";

            ### Read contents of array, Match for ifdef and set the flags
            foreach my $item (@filecontents ){
            # Match for #ifdef tag and set the flag
            if($item =~m/#ifdef/) {
            $endifflag = 0;
            # couter to track nested ifdef
            $ctrToLookForif def++;
            ## splitting with space to check for valid compiler switch
            @splititem= split(" ", $item);
            foreach my $cs (@compiler_swit ches){
            chomp $cs;
            chomp $splititem[1];
            if( $splititem[1] eq $cs and ($ctrToLookFori fdef == 1)){
            $ifdefflag = 1;
            $flag=0;
            last;
            }
            else {
            $flag=1;
            }
            }
            next;
            }
            elsif( $item =~m/#endif/){
            $ctrToLookForif def--;
            if($ctrToLookFo rifdef == 0){
            $endifflag = 1;
            $ifdefflag = 0;
            $flag=0;
            }
            next;
            }
            else {
            if($flag==1){
            next;
            }
            print NEWFH "$item";
            next;
            }
            if($ifdefflag and $endifflag==0){
            print NEWFH "$item";
            }
            if($elseflag and $flag){
            print NEWFH "$item";
            }
            if($endifflag){
            $ifdefflag = 0;
            next;
            }
            }
            close FH;
            close NEWFH;
            ###### end of the file
            [/code]

            Regards
            Pramod
            Last edited by numberwhun; Nov 28 '07, 01:14 PM. Reason: add code tags

            Comment

            • pramodkh
              New Member
              • Nov 2007
              • 23

              #7
              Hi All,

              Any updates on this post? let me know if you need any further details.
              I am still waiting for replies.

              Regards
              Pramod

              Comment

              Working...