perl - calculate the number of lines from particular files

Hi,

plz see the below code.
here my aim is to calculate the number of lines in unprocessedData.out

if this file contains 40 lines then lastly $linenum should print 40.(except blank lines)

i have tried below code but it giving me the output only one. can anyone help me how to do ?

=============================================

#!/usr/bin/perl -w
$ARGV[0]||='/home/user01/UnprocessedData.out';
open OR,$ARGV[0];
while (<OR>) {
  next if /^\s*$/;
  $linenum++;
} 
close OR;

=====================================================

regards,
Priya

 
bash-3.00$ ./test.pl
4

bash-3.00$ cat -n UnprocessedData.out
     1  a
     2  b
     3  c
     4  d
     5  
     6  
     7  
     8  
     9  
    10  

bash-3.00$ cat test.pl
#!/usr/bin/perl -w
$ARGV[0]||='/home/user01/UnprocessedData.out';
$linenum=0;
open OR,$ARGV[0];
while (<OR>) {
next if /^\s*$/;
$linenum++;
} 
print "$linenum\n";
close OR;

Hi itkamaraj,

can u plz explain the code

"bash-3.00$ cat test.pl"

and one more question is that if i am taking UnprocessedData.csv

instaed of UnprocessedData.out

then it gives me the correct output using my code. i think it having an problem with ".out" file

i have tried your code also but it giving me the same output as "1"

can u plz write your code repeate.I didnt understand what u have write in UnprocessedData.out??

Thanks...

 
$: cat -n test
1  a
2  b
3  c
4  d
5  
6  
7  
8  e
9  f
10  
11  
12  g
13  
14  h

$:  perl -e 'while(<>){next if /^\s*$/;$count+=1;} print "$count\n"' test
8

---------- Post updated at 03:04 PM ---------- Previous update was at 02:43 PM ----------

Are you running this script in windows machine ?

---------- Post updated at 03:17 PM ---------- Previous update was at 03:04 PM ----------

$ARGV[0]||='/home/user01/UnprocessedData.out'; --> check any file is passed as argument, if not then take the file /home/user01/UnprocessedData.out
$linenum=0; --> initalize the value to 0
open OR,$ARGV[0]; --> FileHandler OR to open the file
while (<OR>) { --> loop the file upto the last line
next if /^\s*$/; --> skip the next line code if the line is blank
$linenum++; --> once if condition fails, it will increase the variable value (it means, it is not blank line)
} --> end of while loop
print "$linenum\n"; -->print the linenum variable value.
close OR; --> close the fileHandler

Hi,

i have tried but i am not getting the output correctly.

i am using unix server.

Do you get the wrong line count? Do you get an error? Does anything happen at all? You'll have to give us more information if you want useful answers.

On an unrelated note: itkamaraj, I see you 53 character one-liner, and raise you to 27:

perl -e 'printf"%d\n",(grep{/\S/}<>)+0' test

another way,

Tested in windows

perl -le "print ~~grep{/./}<>" test
perl -le "print~~grep/./,<>" test

Nice, but I'd match on \S instead of ., since that's the way the original code was defining an empty line (no printable characters).

Hi.

One method:

   How do I count the number of lines in a file?
       One fairly efficient way is to count newlines in the file. The
       following program uses a feature of tr///, as documented ...

more at man perlfaq5

and a wrapper for that:

NAME
       File::CountLines - efficiently count the number of line breaks in a
       file.

       Since different operating systems have different ideas of what a
       newline is, you can specifiy a "style" option ...

more at perldoc File/CountLines

Good luck ... cheers, drl