Solved

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

Posted on 2011-09-14
10
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

734 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