In linux:
Perl scripts
What would be the search pattern for extracting all the lines that contain at least a 3-digit number in /etc/services, such as the lines that have 110 (pop3) or 143 (imap), but not the lines that contain 1649 (kermit), 3306 (mysql), or 10080 (amanda), etc.
#!/usr/bin/perl
use strict;
my $filename = '/etc/services';
# opening file to read
open my $info, $filename or die "Could not open $filename: $!";
# reading line by line
while( my $line = <$info>) {
if ($line =~ /#/){}
else{
# splitting by space to find 2nd col
my $v = ((split ' ', $line)[1]);
# splitting by / to find process id
my $proceess_id = ((split '/', $v)[0]);
# if length 3 then printing it
if (length($proceess_id)==3){
print $line;
}
}
}
# OUT
Get Answers For Free
Most questions answered within 1 hours.