How do I process lines in a file from the point where a line contains a certain word ?
I have a file that I wish to process. e.g.
a line
another line
internal name
another line
a further line
need this
another line
need this
I want to start processing the file where the line begins with "internal name". After this I only want to process the lines that have "need this" in them until end of file. Where "need this" always occurs at the beginning of the line.
I can't use seek because I don't know how many lines will be in the file and at what position the "internal name" first appears.
I tried doing something like this but it doesn't work
open DATA,filename;
while (<DATA>) {
chomp;
next unless (/^internal name/);
next unless (/^need this/);
do something with data
}
close DATA;
This doesn't work because each time round the loop it searches for "internal name".
These are variable length records per line and the file is a variable number of lines long.
Any help appreciated
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.