I have a file, and I need to take a count value from the file.
I am storing all the contents of the file in an array and later searching for that particular line (Total number of lines in file is). And again using regex, I am taking out the count.
Code below:
#!/usr/bin/perl
use Data::Dumper;
my @array = <DATA>;
print Dumper(\@array);
my $count = 0;
my @matches = grep { /Total number of lines in file is/ } @array;
foreach (@matches){
$count = $1 if($_ =~ /Total number of lines in file is :\s*(\d+)\s*/);
}
print "Count:$count\n";
__DATA__
Below is your data file
This is test line A
This is test line B
This is test line C
This is test line D
This is test line E
This is test line F
This is test line G
This is test line H
This is test line I
This is test line J
Total number of lines in file is :10
Here I am using grep and for loop to take the count which is needed to me.
Is there any better way to do this? So, using single grep I can take the count.