In your program you posted above, where did you use pos?
Also, the pos function does not do what you might think what it does.
| [reply] [d/l] |
You're right, using pos is only giving me the previous line number if I feed it $. Not sure how I would go about placing data to that line number. Any suggestions on a better route to take?
#!/usr/bin/perl
open DATA, "</home/file.conf";
while (<DATA>)
{
if(m/\END\b/)
{
print "$.\n";
print "$_\n";
$pos = $.;
$pos--;
print "$pos\n";
}
}
close(DATA);
produces: Line #, Data, Previous Line # | [reply] [d/l] |
That's a first step in the right direction. Now, you don't want to print the line number of the previous line, but the content of the previous line. So you will need to come up with some way to remember the previous line and output it when the current line matches /^END$/ (I note you wrote /\END\b/, but I assume that's not what you mean). As I outlined several replies before this one, one possible approach could be:
- Check the current line
- If it matches /END/, print the previous line (as remembered)
- Remember the current line as "previous line" in a variable
| [reply] [d/l] [select] |