Dumping some of the PERL scripts that I had written from my home directory into this blog entry
Array:
array_count.pl
Array:
array_count.pl
#!/usr/bin/perl -w # # This script simply gives the length of the array # @numbers = qw(one two three four); $array_length = @numbers; print "Array length = $array_length\n"; |
array_reversal.pl
#!/usr/bin/perl -w # # This script give an example of how an array is reversed in perl # @teams = qw(chelsea liverpool mancity arsenal); foreach my $team (reverse @teams) { print ("$team \n"); } |
Capture command line output:
mysqld_port.pl
#!/usr/bin/perl -w # # This script redirects the mysql infor from netstat into a file # open (MYHANDLE, "netstat -tulpn|"); open (OUTHANDLE, ">> mysql.out"); while(<MYHANDLE>) { if (/mysqld/) { print OUTHANDLE $_; } } close (OUTHANDLE); close (MYHANDLE); |
num_files.pl
#!/usr/bin/perl -w # # This scripts counts the number of files in the current directory # sub file_count { open (MYHANDLER,"ls -1 |"); $count = 0; while (<MYHANDLER>) { $file = $_; chomp($file); if (-f $file) { $count = $count + 1; } } print "There are $count files in the directory\n"; } file_count(); |
open_netstat.pl
#!/usr/bin/perl -w # # This script redirects the output of netstat into a file # open (MYHANDLE, "netstat -tupl|"); open (MYOUT, ">>mynetstat.txt"); while(<MYHANDLE>) { print MYOUT $_; } |
File IO:
check_logfiles.pl
#!/usr/bin/perl -w # # This script accepts log files as input and checks for the string 'connection # accepted from' # while ($data = <>) { if ($data =~ /\bconnection accepted from\b/) { print "$data"; } } |
get_nameserver.pl
#!/usr/bin/perl -w # # This function get the name server ip addresses from the resolv.conf file # sub get_nameserver { open(MYHANDLER, "/etc/resolv.conf") || die ("ERROR: unable to open file\n"); while(<MYHANDLER>) { if(/nameserver\b/) { @ip_addr = split(/\s+/,$_); open(MYOUT,">>outfile"); print OUT "$ip_addr[1]\n"; } } close(MYHANDLER); close(MYOUT); } get_nameserver(); |
number_of_lines.pl
#!/usr/bin/perl -w # # This function asks the user to enter a file name # sub enter_filename { print "Enter a file name: "; $file_name = <STDIN>; chomp($file_name); return($file_name); } # # This function counts the number of lines in the file # sub number_lines { $i = 0; $file_name = $_[0]; if (-e $file_name) { open (MYHANDLER, $file_name) || die("ERROR: unable to open file"; while (<MYHANDLER>) { $i=$i+1; } close(MYHANDLER); return($i); } else { print "File $file_name does not exist.\n"; } } $user_file_name = enter_filename(); $number_of_lines = number_lines($user_file_name); print "Number of lines in the file: $number_of_lines\n"; |
read_hosts.pl
#!/usr/bin/perl -w $file_name = "/etc/hosts"; # # This function returns host names and ip address from the /etc/hosts file # sub host_ipaddr { open(MYHANDLE,$file_name); while(<MYHANDLE>) { chomp; if (/^[0-9]/) { @fields = split(/\s+/,$_); print ("$fields[0] $fields[1]\n"); } } close(MYHANDLE); } host_ipaddr($file_name); |
read_passwd.pl
#!/usr/bin/perl -w # # This function reads the password file and outputs it onto the screen/stdout # sub read_passwd { open (FILEHANDLE, "/etc/passwd") || dir("ERROR: unable to open file\n"); while ($name = <FILEHANDLE>) { chomp($name); print "$name \n"; } close (FILEHANDLE); } read_passwd(); |
remove_blank.pl
#!/usr/bin/perl -w # # This script removed the blank lines from the file passed to it. The out # gets displayed on the screen/stdout. # if (! defined $ARGV[0]) { usage(); } else { $my_file = "$ARGV[0]"; } if (-e $my_file) { print ("File exists\n"); } else { print ("File does not exists.\n"); exit 1; } open (MYHANDLER, $my_file) || dir("ERROR: unable to open file\n"); while(<MYHANDLER>) { if (/[^\s+]/) { print $_; } } close(MYHANDLER); sub usage { print ("USAGE: .\/remove_blank.pl <file_name>\n"); exit 1; } |
write_passwd.pl
#!/usr/bin/perl -w # # This function reads the password file and outputs it onto the file outfile.txt # sub write_passwd { open (FILEHANDLE, "/etc/passwd") || dir("ERROR: unable to open file\n"); open (OUTFILE, ">outfile.txt") || dir("ERROR: unable to open file\n"); while ($name = <FILEHANDLE>) { print OUTFILE $name; } close (FILEHANDLE); close (OUTFILE); } write_passwd(); |
Loops:
print_num.pl
#!/usr/bin/perl -w print "Enter a number: "; $my_num = <STDIN>; chomp($my_num); print_number($my_num); sub print_number { $prtnum = $_[0]; print "$prtnum\n"; for ($i=$prtnum-1; $i >=0 ; $i--) { print_number($i); } } |
Match:
grep_user.pl
#!/usr/bin/perl -w while (<>) { if (/oracle/) { print $_; } } |
number_range.pl
#!/usr/bin/perl -w sub user_interface { print "Enter a number between 1 to 9: "; $my_number = <STDIN>; chomp ($my_number); return $my_number; } sub print_number { my $my_number = $_[0]; if (($my_number >= 1) && ($my_number <= 9)) { print "The number entered is: $my_number\n"; } else { print "The number $my_number is out of range.\n"; } } $num_entered = user_interface(); print_number($num_entered); |
single_digit.pl
#!/usr/bin/perl -w $_ = 32; if (/[0123456789]/) { print "Match: $_ \n"; } |
Split:
greatest_number.pl
#!/usr/bin/perl -w sub user_interface { print "Enter some numbers: "; $numbers = <STDIN>; chomp($numbers); return $numbers; } sub greatest_number { @number_array = split(/\s+/,$_[0]); $x = $number_array[0]; foreach $i (@number_array) { if ($i > $x) { $x = $i; } } print "The greatest number is $x\n"; } $num_entered = user_interface(); greatest_number($num_entered); |
split_ipaddr.pl
#!/usr/bin/perl -w $ip_addr = "129.40.71.195"; @addr = split(/\./,$ip_addr); foreach $num (@addr) { print "$num\n"; } |
split.pl
#!/usr/bin/perl -w # # This script finds if user oracle exists in the /etc/passwd file # open (MYHANDLER,"/etc/passwd"); while (<MYHANDLER>) { @fields = split(/:/,$_); $_ = $fields[0]; if (/oracle/) { print "User oracle exists.\n"; print "$_\n"; } } |
String:
reverse.pl
#!/usr/bin/perl -w $mystring = "hello"; $mylength = length($mystring); @fields = split(//,$mystring); for ($i=$mylength-1; $i>=0; $i--) { print $fields[$i]; } print "\n"; |
strcmp.pl
#!/usr/bin/perl -w sub user_interface { print "Enter the first string: "; $strs = <STDIN>; chomp($strs); return ($strs); } sub string_compare { print "string_compare function $_[0]\n"; @strings = split(/\s+/,$_[0]); print "The strings after splitting: $strings[0] $strings[1]\n"; $len_one = length($strings[0]); $len_two = length($strings[1]); if($len_one != $len_two) { print "The length of the strings is different\n"; } else { print "The length of the strings is same\n"; @first_array = split(//,$strings[0]); @second_array = split(//,$strings[1]); for ($i=0; $i < $len_one; $i++) { if ($first_array[$i] ne $second_array[$i]) { print "The words are different\n"; exit 1; } } print "The words are the same\n"; } } $my_strings = user_interface(); print "The strings to be compared are $my_strings\n"; string_compare($my_strings); exit; |
strcpy.pl
#!/usr/bin/perl -w $orig_string = $ARGV[0]; print ("Original: $orig_string \n"); $final_string = $orig_string; print ("Result: $final_string \n"); [root@isvx3 string_manipulation]# cat strcpy.pl #!/usr/bin/perl -w $orig_string = $ARGV[0]; print ("Original: $orig_string \n"); $final_string = $orig_string; print ("Result: $final_string \n"); |
stringlen.pl
#!/usr/bin/perl -w $myword = "silly me"; $mylen = length($myword); print "Length = $mylen"; |
string_reversal.pl
#!/usr/bin/perl -w $myword = "silly me"; $mylen = length($myword); print "Length = $mylen"; |
upper_lower.pl
#!/usr/bin/perl -w print "Enter a world: "; $myword = <STDIN>; chomp($myword); $myword =~ s/\W.*//; print "The word entered is: $myword \n"; |
user_exists.pl
#!/usr/bin/perl -w $file = $ARGV[0]; if (-e $file) { open(MYHANDLER, $file); while(<MYHANDLER>) { chomp; if (/\bHadoop\b/) { print "$_ \n"; } } close(MYHANDLER); } else { print "File $file does not exist\n"; } |
wordcountmap.pl
#!/usr/bin/perl -w while (<STDIN>) { chomp; @words = split(/\s+/,$_); foreach $w (@words) { print "$w\n"; } } |
No comments:
Post a Comment