Sunday, 31 August 2014

PERL PROGRAM TO COUNT nunber of Words, lines and charachters in a file

// Demo.pl


$line=0;
$word=0;
$char=0;

print("\n Count Line,word,character");
open(FILE,"Demo1.sh");
while(<FILE>)
{
$line++;
$char+=length($_);
$word+=scalar(split(/\W+/,$_));
}

print("\nline   word   char  ");
print("\n$line   $word  $char \n");

close(FILE);

******************Sample OUTPUT**********************
//My program is saved on desktop
//Demo1.sh is input file...as per need change name of input file which is located in same directory

svcet@svcet-MS-7636:~$ cd Desktop
svcet@svcet-MS-7636:~/Desktop$ perl Demo.pl

 Count Line,word,character
line   word   char 
2   11  61

No comments:

Post a Comment