Solved

How to identify the beginning of a section in a file with a keyword?

Posted on 2011-09-14
10
251 Views
Last Modified: 2012-05-12
Hi,
This is a follow up question for ID: 27285438.

In this script I missed one point. Actually, my text file includes many other things in it. And the beginning of the section that I am interested in starts with this type of line:


                         
           Submit file
    ===========================
and then here is the part that I parse

Open in new window


So can you please modify the code in other question ID: 27285438 to capture the beginning of it. And if it does not exist at all then we don't even need to parse the file.

How can I do that?

Thanks,


0
Comment
Question by:Tolgar
  • 5
  • 3
  • 2
10 Comments
 
LVL 9

Expert Comment

by:parparov
ID: 36539396
Is this grammar fixed?
i.e. optional whitespace, then Submit (capitalized) file (is it a keyword or any file name may be here),
then next line a bunch of equal signs between whitespaces?

0
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 100 total points
ID: 36539418
for ( @rippedParagraphs ) {
  my $submit_file;
   if (/\s*Submit\sfile/) {
       $submit_file=1;
   }
   if ($submit_file==1) {

        if(m|^#\s*Sandbox\s+location\s*\:\s*/sandbox/(.*?)/|) {
               $sandbox_location = $1;
               print $sandbox_location, "\n";
        }
        if (/^\#/ || !/\S/) {
            next unless $opt_flag || $file_flag;
        ...
        ...
        /^CR:/ && push(@CR, $_) && next;
        /^CS:/ && push(@CS, $_) && next;
        /^RR:/ && push(@RR, $_) && next;
        $opt_flag ? push(@Options, $_) : push(@Files, $_);
      }
  }
0
 
LVL 9

Expert Comment

by:parparov
ID: 36539627
I'd rather wait for Tolgar's grammar clarification.
0
 

Author Comment

by:Tolgar
ID: 36539632
@parparov: Yes this grammer is fixed. "file" is the keyword. Then optional white space before a bunch of equal signs.

@wesly_chen: I think you ignored the equal signs in the next line.


I am waiting for your reply,

Thanks,

 
0
 

Author Comment

by:Tolgar
ID: 36539652
So let me make it more clear:

[any number of white spaces]Submit file
[any number of white spaces][any number of equal signs] 

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:parparov
ID: 36539665
Then the solution would be the slightly adjusted wesley_chen's code:
 my $submit_file = 0;
for ( @rippedParagraphs ) {
   if (/\s*Submit\s+file/) {
       $submit_file++;
       next;
   }
   if ($submit_file == 1) {
      if(/\s*\=+/) {
        $submit_file++;
      }
      else {
        $submit_file = 0; # two-line grammar didn't hold
      }
      next;
   }
   if ($submit_file == 2) {
        if(m|^#\s*Sandbox\s+location\s*\:\s*/sandbox/(.*?)/|) {
               $sandbox_location = $1;
               print $sandbox_location, "\n";
        }
        if (/^\#/ || !/\S/) {
            next unless $opt_flag || $file_flag;
        ...
        ...
        /^CR:/ && push(@CR, $_) && next;
        /^CS:/ && push(@CS, $_) && next;
        /^RR:/ && push(@RR, $_) && next;
        $opt_flag ? push(@Options, $_) : push(@Files, $_);
      }
  }
	 	
	

Open in new window

0
 
LVL 9

Accepted Solution

by:
parparov earned 400 total points
ID: 36539685
Minor polishing - the ifs should match from the beginning of the string, and garbage should not trail
 my $submit_file = 0;
for ( @rippedParagraphs ) {
   if (/^\s*Submit\s+file\s*$/) {
       $submit_file++;
       next;
   }
   if ($submit_file == 1) {
      if(/^\s*\=+\s*$/) {
        $submit_file++;
      }
      else {
        $submit_file = 0; # two-line grammar didn't hold
      }
      next;
   }
   if ($submit_file == 2) {
        if(m|^#\s*Sandbox\s+location\s*\:\s*/sandbox/(.*?)/|) {
               $sandbox_location = $1;
               print $sandbox_location, "\n";
        }
        if (/^\#/ || !/\S/) {
            next unless $opt_flag || $file_flag;
        ...
        ...
        /^CR:/ && push(@CR, $_) && next;
        /^CS:/ && push(@CS, $_) && next;
        /^RR:/ && push(@RR, $_) && next;
        $opt_flag ? push(@Options, $_) : push(@Files, $_);
      }
  }

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36539689
oh you are very quick.

I was gonna add something else. This "Submit file" and equal sign pattern can be repeated in the file. So in this case I would like to capture all instances of it.

Is this code gonna do that?

Thanks,
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36539691
Does  ”===========================“ always have 27 "=" characters?
0
 
LVL 9

Expert Comment

by:parparov
ID: 36539719
Change line 4 to
$submit_file = 1

Open in new window

Then you will catch all instances
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now