On Tuesday, June 18, 2002, at 08:46 , Shishir K. Singh wrote:

> Is there a way to get the number of lines in a file. Conditions:
>
> a) Without using backticks on wc -l
> b) Without opening the file and looping over the records


not really unless your OS provides some sort of 'meta-data'
file that you could open and it had such in it.

the notion of a 'line' for a 'file' is rather an arbitrary
construct that we have taken to mean

        that which ends with the EOL token of this OS

remember that 'wc -l' is really little more than a
fancy way of counting the EOL tokens in a file.

note that the sequence

        "\n\n"

would be considered 'two lines' - one of which is 'blank'.

IF you knew that the file were a fixed format structure
such that there were exactly K bytes of data per line,
then you could use stat() or lstat() to return the size
and divide by K to calculate the data.

So if you want an accurate count - then opening the
file and spinning through it would be a way to get that value
        my $count=0;
        open(FH, $someFile) or die "unable to open $someFile: $!\n";
        $count++ while(<FH>);
        close FH;


ciao
drieux

---

for fun, understand

[jeeves:/tmp/drieux] drieux% od -c file
0000000    l   i   n   e   1  \n   l   i   n   e   2  \n   l   i   n   e
0000020    3  \n  \n   l   i   n   e   4  \n
0000031
[jeeves:/tmp/drieux] drieux%  wc -l file
        5 file
[jeeves:/tmp/drieux] drieux% grep -c '\n' file
4
[jeeves:/tmp/drieux] drieux%


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to