Connecting Tech Pros Worldwide Forums | Help | Site Map

Processing a portion of a file.

Newbie
 
Join Date: Jul 2007
Posts: 3
#1: Jul 30 '07
Hi ,

I want to grep for multiple lines, like

;START
;copy here
;END

I need to grep from ;START to ;END
I am using windiws XP.

Please let me know if anybody can help.

I tried
grep /^;START[.*][\n];END$/mi, <FILE>;


Thanks
Anil

miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: Jul 30 '07

re: Processing a portion of a file.


Take a look at the Range Operator on perldoc, and do your file processing using a while loop.

- Miller
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Jul 30 '07

re: Processing a portion of a file.


you need to return the value of grep to an array:
Expand|Select|Wrap|Line Numbers
  1. my @lines = grep /^;START[.*][\n];END$/mi, <FILE>; 
[.*] is a character class of a dot and an asterisk, not the same as (.*) which captures the pattern in memory, and [\n] is not needed if this really is a multiline match.

try like this:

Expand|Select|Wrap|Line Numbers
  1. my @lines = grep /^;START(.*);END$/mi, <FILE>;
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#4: Jul 30 '07

re: Processing a portion of a file.


Expand|Select|Wrap|Line Numbers
  1. my @lines = grep {/^;START/../^;END/} <DATA>;
  2.  
  3. print @lines;
  4.  
  5. __DATA__
  6. more stuff
  7. and yet
  8. ;START
  9. ;copy here
  10. ;END
  11. final stuff
  12. and yet
  13.  
- Miller
Reply