It sounds like you just want a simple script to open a file,
find the line that says "total", and report the last two rows
of numbers.
Here is some code to do this :
#!/usr/local/bin/perl
print "Input file name: ";
$_ = <>;
chop;
open(INFILE, $_);
while (<INFILE>)
{
if (m/total/)
{
s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/;
print;
}
}
close INFILE;
There are probably 3 things in this script that are non-intuitive
to the budding Perl programmer :
1) The use of $_
2) The use of
<>
3) The use of regular expressions
Here they are, demystified :
1) $_ is the "default variable" when you don't supply any other
variable name to a sub (such as chop and print in our example).
2)
<> abbreviates
<STDIN>
3) There are two regular expressions in this example, the first
one is a matching regular expression (m/total/) which returns true
if and only if the text "total" exists as a substring of $_
The second regular expression is a search and replace regex.
s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/
all it does is replace "^total\sA\sB\sC\sD$" with "C D" where
^ (caret) matches start of line, \s matches space,
and A B C and D are numbers (1 or more digits). The use of
the parenthesis following the first / provides Perl's regular
expression engine the ability to do what is called "backreferencing"
(using $1 and $2 after the second /).
Let me know if that code works for you, and if there's anything
else you'd like to do.
-verbal
</CODE>
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.