Sunday, June 8, 2014

How do you count number of lines in a file?

my $msg;

open(IN, "<", "$0") or die "Can't open $0 - $!";
while (<IN>) {
$msg = $msg . $_;
}
close IN;

$count = () = $msg =~ /\n/g;
print "Line Count - $count\n";

#or

$lines = () = $msg =~ m/\n/g;
print "Line Count - $lines\n";

How do you replace either of the words with a new word?

if ($line =~ m/O(RC|BR)/) {
$line =~ s/$&/\n\n$&/ig;
}

will replace either ORC or OBR with "\n\nORC" or "\n\nOBR".