How do you write a perl script called myscript.pl to determine how many logins occurred for user "abc123456" from myusersfile.txt?
content in mysuerfile.txt are user id "abc123455", user id "abc123456" and user id "abc123457". But we want to know how many times did user id abc123456 login the system.
Executable Code:
my $file = 'myusersfile.txt';
my $word= 'abc123456';
$count =0;
# Open the file
open my $fh, '<', $file or die "Could not open '$file'
$!\n";
# Read each line
while (my $row = <$fh>) {
chomp $row;
# If abc123456 found
if ($row eq $word) {
#Increment count
$count++;
}
}
print "The number of logins for user abc123456 is ";
print $count;
print "$!\n"
Get Answers For Free
Most questions answered within 1 hours.