#!/usr/bin/perl use warnings; # it warns about undefined values use strict; # it's a lexical scoped declaration use Data::Dumper; use constant ARGUMENTS => scalar 3; $| = 1; #flushing output my $filename; my $Counting = 0; my $Characters = 0; my $source = $ARGV[0]; my $patern = $ARGV[1]; my $target = $ARGV[2]; my $elements; my $value; my $lines; my @found; my @word; #my $path_source = "/path/".$source.""; #my $path_destination = "/path/".$target.""; open(READ, ,"<", $source) or die "Can not open ".$source.": $!\n"; # < read mode open(WRITE, ,">", $target) or die "Can not open ".$target.": $!\n"; # > write mode if (@ARGV > ARGUMENTS) { print "Too many argument inputs\nCorrect syntax ".$source." ".$patern." name of the file to store the data.\n"; } elsif (@ARGV < ARGUMENTS) { print "Need more argument inputs\nCorrect syntax ".$source." ".$patern." name of the file to store the data.\n"; } else { while ($lines = ) { chomp ($lines); # chomp avoid \n on last field @word = split ((/\s+/), $lines); #\s+ = 1 or more white-space characters # \d+ matches one or more integer numbers foreach $value (@word) { if ($value eq $patern) { push (@found, $value); print "I have found one matching pattern: $value\n"; } } } $elements = scalar(@found); print "I found number of occurrences: ".$elements."\n"; print WRITE "I found pattern: $patern, number of occurrences: $elements\n"; } close (READ) or die "READ did not close: $!\n"; close (WRITE) or die "WRITE did not close: $!\n"; $| = 1; #flushing output