Perl regex using /START/../END/


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regex using /START/../END/
# 1  
Old 01-10-2011
Question Perl regex using /START/../END/

I need help with perl code. I have a data file with lots of data

example: data.txt file
Code:
START
DATA1 sjdfh kjhdf 
DATA2 sdkjfhk jds dshfgdf 
... Around 20 - 30 lines
END
LDDDD awdkjasd a sdkahgdk jasdh
SOME CRAP

Some EMPTY lines etc 
START
DATA1 sjdfh kjhdf 
DATA2 sdkjfhk jds dshfgdf 
... Around 20 - 30 lines
END
SOME EMPTY Lines
SOME MORE CRAP 
START
DATA1 sjdfh kjhdf 
 DATA2 sdkjfhk jds dshfgdf 
 STOP

START
DATA1 sjdfh kjhdf 
  DATA2 sdkjfhk jds dshfgdf 
  END


Now in Data file I need to extract any line witH START and end with END or STOP

when I use /START/../END/ I can extract files but it does not have all the values I need .


Also I am using
$* # Enable multi line pattern etc.

Any quick snippet will help . Thanks for reading the post.
# 2  
Old 01-10-2011
This will save all the lines which starts with START and ends with END and STOP to another file.

Code:
#!/usr/bin/perl
$file ="test.txt";
open(INFILE,$file);
$i=0;
while (<INFILE>){
push (@arr1,split(/\n/,$_));
}
for ($i=0;$i<$#arr1;$i++) {
  $myline = $arr1[$i];
   $j=$i;
   if ($myline =~/START/i) {
     until(($myline =~/END/) || ($myline =~/STOP/)) {
      $myline = $arr1[$j];
      push(@arr,$myline);
      $j=$j+1;
    }
}
}
close INFILE;
$file1="log.txt";
open(OUTFILE,">",$file1);
foreach $val(@arr) {
  print OUTFILE $val."\n";
}
close OUTFILE;


Last edited by pludi; 01-10-2011 at 03:57 AM..
# 3  
Old 01-10-2011
Thank you SIR def123 This does what I need .. Thanks Smilie

Only thing if I have START also mutiple start then
example
START and START ONE or ANOTHER ONE etc ...
I am playing around with
Code:
if (($myline =~ /START/ ) || ( ...

I guess it is correct ...

Thank you again for your code Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

4. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

5. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. UNIX for Advanced & Expert Users

Complete dump from start to the end

Hi all... I was accidentally wipe off my iphone and lost all the data in it:(. So I tried to use dd to create an image from iphone and ssh to the Mac by using: dd if=/dev/disk | ssh user@MacIP of='image.img' Then I mount this image on Mac/Windows and run recovery software because most of the... (1 Reply)
Discussion started by: seraphc
1 Replies
Login or Register to Ask a Question