in reply to Re^6: Reading multiple lines from text file
in thread Reading multiple lines from text file
Hello again newperlbie,
(1) Please read Markup in the Monastery “Chapter 1” and update your post accordingly.
(2) “in fact that was my previous comment, but I removed it”
In future, please use <strike>...</strike> tags around discarded text rather than deleting it. Also clearly mark any updates as such. This will reduce the likelihood of confusion for responders to your posts.
(3) “I figured a way to grep it from the middle of the line.”
Good! (But then, why the next question?)
(4) “The key column can be any where ,not necessarily last....what should be the adaptation now.”
Can the key column change from one line to another? Is it guaranteed that the key words will never appear accidentally in non-key positions within a line? If so, you can just test for the presence of a keyword anywhere in the line with a simple regular expression:
while (<DATA>) { if (/\b$first\b/ .. /\b$last\b/) { $in = 1; print; } elsif ($in && /\b$last\b/) { print; } else { $in = 0; } }
(In a regex, \b matches a word boundary.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|