0

I am looking for help to extract particular line which has "Start" and "End" Keyword and Input file:

adfafaf
adfafafas
adfafaf
Detail=Process Start Time   : TIMESTAMP '2014-02-14 01:20:58.918757'
AAAAAAAA
BBBBBBBB
CCCCCCCC
DDDDDDDD
End Time                    : TIMESTAMP '2014-02-14 01:21:49.520818'
adffffffff
adfffff
adfff

Desired output: Below lines are required to moved to string/array.

Detail=Process Start Time   : TIMESTAMP '2014-02-14 01:20:58.918757'
AAAAAAAA
BBBBBBBB
CCCCCCCC
DDDDDDDD
End Time                    : TIMESTAMP '2014-02-14 01:21:49.520818'
Miller
  • 34,962
  • 4
  • 39
  • 60
Naga
  • 347
  • 2
  • 16
  • what's wring with just iterating over the file line-by-line and when a line matches, you push it to the array (or do whatever you want with it)? Takes about 5 lines of code – Martin Drautzburg May 04 '14 at 20:07
  • `@array = eval('sub { grep /Start/../End/, @_ }')->( )` – ysth May 04 '14 at 20:09
  • See also http://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl – Martin Drautzburg May 04 '14 at 20:18
  • @ysth, How that's different than `@array = grep /Start/../End/, `? – ikegami May 04 '14 at 21:14
  • 1
    @ikegami: not, if you only ever run it once or there is never a Start without an End – ysth May 04 '14 at 22:02
  • @ysth, I see. You'd want to use your version inside of a subroutine to make sure the iterator is reset. I don't think the `sub` is necessary for that, though. `my @array = eval('grep /Start/../End/, '); die $@ if $@;` should do. But wouldn't be simpler just to avoid `..`? – ikegami May 04 '14 at 22:19
  • @ikegami: yes, it would be simpler to avoid .. :) – ysth May 04 '14 at 22:30

1 Answers1

0

This gets your lines

if ($subject =~ m/^[^\n]*?Start.*?End[^\n]+/sm) {$result = $&;}

It is dot-all mode so the .* can match over multi lines. To stop at the end of the last line we use [^\n]+ which match all character except newline.

Hans Schindler
  • 1,787
  • 4
  • 16
  • 25
  • you want `[^\n]` instead of the first `.` (or use `.` everywhere but `(?s:.*)` in the middle) – ysth May 04 '14 at 22:07