Perl Scripts

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16
At a glance
Powered by AI
The documents provide examples of Perl scripts that can be used to automate common system administration tasks like image processing, file renaming/conversion, and searching/finding files.

Some common tasks that can be automated using the example Perl scripts include combining images, renaming files, converting between file formats, creating directories, padding/unpadding file numbers, finding free machines/users/processes, and searching/finding files and users.

The 'findfile' script searches all subdirectories of a given directory to find a specified file.

Image Processing

#!/usr/local/bin/perl # # composite series of images over a background image # if ($#ARGV != 4) { print "usage: compem bg.rgb inbase outbase startNum stopNum\n"; exit; } $bg = $ARGV[0]; $inbase = $ARGV[1]; $outbase = $ARGV[2]; $start = $ARGV[3]; $stop = $ARGV[4]; # for each image for ($i=$start; $i <= $stop; $i++) { # pad numbers $num = $i; if($i<10) { $num = "00$i"; } elsif($i<100) { $num = "0$i"; } # call unix command "over" $cmd = "over $bg $inbase.$num $outbase.$num 0 0"; print $cmd."\n"; if(system($cmd)) { print "over failed\n"; } }

Renaming Files
#!/usr/local/bin/perl # # rename series of frames # if ($#ARGV != 3) { print "usage: rename old new start stop\n"; exit; } $old = $ARGV[0]; $new = $ARGV[1]; $start = $ARGV[2]; $stop = $ARGV[3]; for ($i=$start; $i <= $stop; $i++) { $num = $i; if($i<10) { $num = "00$i"; } elsif($i<100) { $num = "0$i"; } $cmd = "mv $old.$num $new.$num"; print $cmd."\n";

if(system($cmd)) { print "rename failed\n"; } }

File Conversion
#!/usr/local/bin/perl # # convert series of images from one format to another # if ($#ARGV != 5) { print "usage: fconvert intype outtype old new start stop\n"; exit; } $intype = $ARGV[0]; $outtype = $ARGV[1]; $old = $ARGV[2]; $new = $ARGV[3]; $start = $ARGV[4]; $stop = $ARGV[5]; for ($i=$start; $i <= $stop; $i++) { $num = $i; if($i<10) { $num = "00$i"; } elsif($i<100) { $num = "0$i"; } $cmd = "imgcvt -i $intype -o $outtype $old.$num $new.$num"; print $cmd."\n"; if(system($cmd)) { print "imgcvt failed\n"; } }

Creating Directories
#!/usr/local/bin/perl # # create a series of directories # if ($#ARGV != 2) { print "usage: mkdirs base start stop\n"; exit; } $base = $ARGV[0]; $start = $ARGV[1]; $stop = $ARGV[2]; for ($i=$start; $i <= $stop; $i++) { $num = $i; if($i<10) { $num = "00$i"; } elsif($i<100) { $num = "0$i"; } $cmd = "mkdir $base$num";

print $cmd."\n"; if(system($cmd)) { print "mkdir failed\n"; } }

Padding & Unpadding Files


#!/usr/local/bin/perl # # pad file numbers with zeros # if ($#ARGV != 2) { print "usage: pad base start stop\n"; exit; } $base = $ARGV[0]; $start = $ARGV[1]; $stop = $ARGV[2]; for ($i=$start; $i <= $stop; $i++) { $num = $i; if($i<10) { $num = "00$i"; } elsif($i<100) { $num = "0$i"; } $cmd = "mv $base$i $base$num"; # to unpad, use this instead: # $cmd = "mv $base$num $base$i"; print $cmd."\n"; if(system($cmd)) { print "pad failed\n"; } }

Finding Free Machines


#!/usr/local/bin/perl # # search list of machines for machines with no users logged on # $machines = `systems sgi`; chop($machines); @sgis = split(/ /, $machines); @sgis = sort(@sgis); foreach $machine (@sgis) { if(!(`rusers $machine`)) { print "$machine\n"; } }

Finding Processes
#!/usr/local/bin/perl # # search for processes running on machines # if ($#ARGV != 0) { print "usage: findprocess process\n"; exit; } $process = $ARGV[0]; $machines = `systems sgi`; chop($machines); @sgis = split(/ /,$machines); @sgis = sort(@sgis); foreach $machine (@sgis) { print "Checking $machine...\n"; @lines = `rsh $machine \"ps -ef | grep $process | grep -v findprocess | grep -v grep\"`; if(@lines) { foreach $line (@lines) { $line =~ /^\s*(\w+)\s+(\d+)/; $user = $1; $pid = $2; print "$user on $machine pid: $pid\n"; } } }

Finding Files
#!/usr/local/bin/perl # # search for a file in all subdirectories # if ($#ARGV != 0) { print "usage: findfile filename\n"; exit; } $filename = $ARGV[0]; # look in current directory $dir = `pwd`; chop($dir); &searchDirectory($dir); sub searchDirectory { local($dir); local(@lines);

local($line); local($file); local($subdir); $dir = $_[0]; # check for permission if(-x $dir) { # search this directory @lines = `cd $dir; ls -l | grep $filename`; foreach $line (@lines) { $line =~ /\s+(\S+)$/; $file = $1; print "Found $file in $dir\n"; } # search any sub directories @lines = `cd $dir; ls -l`; foreach $line (@lines) { if($line =~ /^d/) { $line =~ /\s+(\S+)$/; $subdir = $dir."/".$1; &searchDirectory($subdir); } } } }

Finding Users
#!/usr/local/bin/perl # # check whether user is logged on # if ($#ARGV != 0) { print "usage: finduser username\n"; exit; } $username = $ARGV[0]; $machines = "insanity ".`systems sgi`; chop($machines); @machines = split(/ /,$machines); @machines = sort(@machines); foreach $machine (@machines) { if(`rusers $machine | grep $username`) { print "$username logged on $machine\n"; } }

Generating HTML Files


#!/usr/local/bin/perl # # create n html files linked together in slide show # if ($#ARGV != 1) { print "usage: htmlslides base num\n"; exit; } $base = $ARGV[0]; $num = $ARGV[1]; for ($i=1; $i <= $num; $i++) { open(HTML, ">$base$i.html"); if($i==$num) { $next = 1; } else { $next = $i+1; } print HTML "<html>\n<head>\n<title>$base$i</title>\n</head>\n<body>\n"; print HTML "<a href=\"$base$next.html\"><img src=\"$base$i.jpg\"></a>\n"; print HTML "</body>\n</html>\n"; close(HTML); }

Generating Xpost Scripts


#!/usr/local/bin/perl # # generate an xpost script to adjust saturation, and run xpost # if ($#ARGV != 2) { print "usage: fixsat infile.tiff outfile.tiff satval\n"; exit; } $infile = $ARGV[0]; $outfile = $ARGV[1]; $satval = $ARGV[2]; # open xpost script open(XPOST, ">__tmp.xp"); # set view to register A print XPOST "view A\n"; # load original image into reg A print XPOST "load $infile\n";

# run Kmult to turn down saturation print XPOST "Kmult $satval $satval $satval # set view to register B print XPOST "view B\n"; # save unsaturated image print XPOST "save tiff $outfile\n"; # close xpost script close(XPOST); # run xpost script $cmd = "xpost -q -s __tmp.xp"; print $cmd."\n"; system($cmd); # clean up $cmd = "/bin/rm -f __tmp.xp"; print $cmd."\n"; system($cmd);

1.0 a b\n";

Modifying Text Files


#!/usr/local/bin/perl # # change all occurances of a string in a file to another string # if ($#ARGV != 3) { print "usage: chstring oldfile newfile oldstring newstring\n"; exit; } $oldfile = $ARGV[0]; $newfile = $ARGV[1]; $old = $ARGV[2]; $new = $ARGV[3]; open(OF, $oldfile); open(NF, ">$newfile"); # read in each line of the file while ($line = <OF>) { $line =~ s/$old/$new/; print NF $line; } close(OF); close(NF);

Convert Raw Timecode Data to Readable Data

#!/usr/local/bin/perl # # Change raw timecode data to different format # # timecode data event looks like: # # Event: 1 # 00:01:05:23 # 00:01:27:21 # a-2-9 # # Event: 2 # 00:01:56:13 # 00:02:03:19 # a-3-9 # # ...and so on... # # Want to change it to the form: # # a-2-9 = 21.93 seconds = 658 frames # a-3-9 = 7.20 seconds = 216 frames # open(FP,"<log.txt"); $first = 1; $total = 0; while($line = <FP>) { if ($line =~ /^\d\d/ && $first) { $in = $line; $first = 0; } elsif ($line =~ /^\d\d/ && !$first) { $out = $line; $first = 1; } elsif ($line =~ /^\w-/) { $shot = $line; chop($shot); # parse timecodes and # translate in and out into seconds $in =~ /(\d\d):(\d\d):(\d\d):(\d\d)/; $hrs = $1; $mns = $2; $scs = $3; $fms = $4; $inSecs = $hrs * 3600 + $mns * 60 + $scs + $fms / 30; $out =~ /(\d\d):(\d\d):(\d\d):(\d\d)/; $hrs = $1; $mns = $2; $scs = $3; $fms = $4; $outSecs = $hrs * 3600 + $mns * 60 + $scs + $fms / 30; # calc duration $dur = $outSecs - $inSecs; $total += $dur;

# print line printf("$shot = %.2f seconds = %d frames\n", $dur, $dur * 30); } } print "total = ".($total / 60)." mins\n"; close FP;

1. compavg.pl This is a simple script that computes the average of each column in a table of data. It shows one common usage : read in the data and split each line directly to "words" and store these words in an array.

#!/usr/local/bin/perl $count = 0; while (<stdin>) { @w = split; $count++; for ($i=0; $i<=$#w; $i++) { $s[$i] += $w[$i]; } } for ($i=0; $i<=$#w; $i++) { print $s[$i]/$count, "\t"; } print "\n";

2. docsubset.pl This script extracts a subset of docs from a database. The target doc id is specified in a separate file. It shows you how to use the associative array (hash table) to store the target IDs. It also shows you a common way of detecting the beginning and end of a document through pattern matching.
#!/usr/local/bin/perl

open(C, "$ARGV[0]") || die "can't open candidate doc id list file:$ARGV[0]\n"; while (<C>) { /([^\s]+)/; $dict{$1}=1; } close(C); while (<stdin>) { if (/<DOC\s+([^\s>]+)/) { $docID = $1; } elsif (/<\/DOC>/) { if (defined $dict{$docID}) { print "<DOC $docID>\n"; print "$docText\n"; print "<\/DOC>\n"; } $docText =""; $docID =""; } else { $docText .= $_; } }

3. do.pl This is like the "for" or "foreach" loop in a shell script, but it allows you to do some command with different numerical parameter values specified in a file. It shows you how you can dynamically generate a command string and execute it with shell. In general, you can run any command by calling a system function.

#!/usr/bin/perl while (<stdin>) { if (/^\s*([0-9]+)/) { $cmd = "$ARGV[0] $1 " . "$ARGV[1]"; print STDERR "$cmd\n"; system($cmd); } }

4. replace.pl

This script does "string replacement" on the standard input. You specify the rules for translation in a file. The script would load the rules and store them in a hash table, and then replace any matched strings in the standard input. It shows you how to handle arguments including "-help". It's also a good example of how to iteration over all the entries in a hash table.

#!/usr/local/bin/perl use English; use Carp; use Getopt::Long; sub Usage{ my $message = shift; print STDERR $message, "\n" if $message; print STDERR "\nUsage: $0 -d(ef) definition_file < source > newsource\n"; print STDERR <<'EOM'; -d(ef) filename : Specifies the definition file which is a set of pairs, each corresponding to a "replacement pattern", e.g., a A b B ... z Z would replace all lower cases with upper cases -h(elp) EOM exit(1); } if (! &GetOptions("help", "def=s") or $opt_help) { &Usage(); } open(D, $opt_def) || die "can't open definition file:$opt_def\n"; while (<D>) { ($oldp, $newp) = split; $dic{$oldp}=$newp; } close(D); $oldStr = ""; $newStr =""; while (<STDIN>) { $oldStr = $_; foreach $k (keys %dic) { s/$k/$dic{$k}/g; } : display this message

$newStr = $_; if ($oldStr ne $newStr) { print STDERR "\n"; print STDERR "old>>$oldStr"; print STDERR "new>>$newStr"; } print; }

5. lemursrcfmt.pl This is a relatively complex script, which formats any documents of the TREC format (thus any TREC documents, including the Web data) and converts them to the basic Lemur format. It also does some tokenization, such as lower all the cases, removing non-digit-alphabet symbols, etc. Plus, you can control if you want to ignore some tagged fields. The most interesting part of this script is where it parses a stream of text using just one pattern matching statement:
while (/(<\/[^>]+>)|(<[^>]+>)|([^><]+)/go) { $closeTag=$1; $beginTag=$2; $content=$3; ....

This is a *very* powerful method! There are 3 patterns specified with a logic OR operator, so the condition would be true as long as one of them is matched. The option "go" means global and optimal. Optimal is solely for efficiency, but global allows us to apply this pattern to the *whole* string to be matched. Every time, one of the three patterns would be matched, and we use other variables such as "$closeTag" to obtain the matched value. We can then test which of the three is non-empty to decide what token we actually have matched. These should be enough for you to start taking advantage of the power of perl! Believe me, you'll save more time in the long run, if you spend some time to learn it now, as it would help you do many tasks much more quickly... I think a good and fun way of learning it is to first read about some basics; there are a lot of good online tutorials. Then, start *modifying* some existing examples such as what I've listed here, or any other simple perl code examples, to see what's going to happen; again, there are many sample

perl scripts on the Web. Of course, let me know if you run into any trouble or have any questions. I'd be happy to provide you with a sketch of the scripts if you tell me what kind of task you would like to do; you can then try to figure out how to get the details working.

#!/usr/bin/perl use English; use Carp; use Getopt::Long; $debug=0; sub Usage{ my $message = shift; print STDERR $message, "\n" if $message; print STDERR "\nUsage: $0 -d(atabase) DatabaseName -m(ode) Mode\n"; print STDERR <<'EOM'; formatted source is written to standard -d(atabase) output

database to format (e.g., source) a format file with the same name as the database file and with the extension ".fmt" is assume dto exist (e.g., source.fmt), and it provides tag specification The fields recognized in the format file include TITLE, IGNORE, DOCBEGIN, DOCEND, IDBEGIN, IDEND An example of the format file is: IGNORE: tag-to-ignore IDBEGIN: DOCID IDEND: /DOCID IGNORE causes the content tagged by "tag-to-

ignore" (i.e., anything between <tag-to-ignore> and </tag-to-ignore>) to be ignored. There can be multiple "IGNORE" entries. -m(ode) ignore ignoreonly : ignore some fields removeonly: remove tags fullfmt: do all mode: fmtonly : do not remove extra tags, do not

-h(elp)

: display this message

EOM exit(1); } if (! &GetOptions("help", "database=s", "mode=s") or $opt_help) { &Usage(); } open(DBFMT, $opt_database . ".fmt") || die "can't open format file:$opt_database",".fmt\n"; $docBegin="DOC"; $docEnd="\/DOC"; $idBegin="DOCNO"; $idEnd="\/DOCNO"; while (<DBFMT>) { print STDERR if $debug; if (/^\s*TITLE\s*:\s*([^\s]+)/) { $title{$1}=1; } elsif (/^\s*IGNORE\s*:\s*([^\s]+)/) { $ignore{$1}=1; } elsif (/^\s*DOCBEGIN\s*:\s*([^\s]+)/) { $docBegin=$1; } elsif (/^\s*DOCEND\s*:\s*([^\s]+)/) { $docEnd=$1; } elsif (/^\s*IDBEGIN\s*:\s*([^\s]+)/) { $idBegin=$1; } elsif (/^\s*IDEND\s*:\s*([^\s]+)/) { $idEnd=$1; } } #print STDERR "=$idBegin=$idEnd\n"; close (DBFMT); # $debug =1; $removeTag= ($opt_mode eq "removeonly") || ($opt_mode eq "fullfmt"); $ignoreTag= ($opt_mode eq "ignoreonly") || ($opt_mode eq "fullfmt"); open(DB, $opt_database) || die "can't open database\n"; while (<DB>) { print STDERR if ($debug); while (/(<\/[^>]+>)|(<[^>]+>)|([^><]+)/go) { $closeTag=$1; $beginTag=$2; $content=$3; print STDERR "===$1 $2 $3===\n" if ($debug); # $1 is closing tag, $2 is beginning tag, $3 is field content if ($beginTag =~ /<$docBegin>/o) { $docTitle=""; $docID=""; $docBody=""; } elsif ($closeTag =~ /<$docEnd>/o) {

$docID =~ s/\_/X/g; $docID =~ s/\s*$//g; # print "***\n$docID\n"; print "<DOC $docID>\n"; $docTitle =~ s/[^a-zA-Z\s]+//g; $docBody =~ s/[^a-zA-Z\s]+//g; if ($docTitle eq "") { print "<TITLE> No Title </TITLE>\n"; } else { print "<TITLE> $docTitle </TITLE>\n"; } print "<TEXT>\n $docTitle \n$docBody\n</TEXT>\n<\/DOC>\n"; } elsif ($beginTag=~ /<$idBegin>/o) { $inDOCNO=1; } elsif ($closeTag =~ /<$idEnd>/o ) { $inDOCNO=0; } elsif (($beginTag=~/<([^>\s]+)>/o) && $title{$1}) { # beginning of title $inTITLE=1; } elsif (($closeTag=~/<\/([^>\s]+)>/o) && $title{$1}) { $inTITLE=0; } elsif ($inIGNORE && ($closeTag=~/<\/([^>\s]+)>/o) && ($1 eq $ignoreTagName )) { print STDERR "finding ignore closing tag: $closeTag\n" if ($debug); $inIGNORE=0; $ignoreTagName=""; $ignoredTxt .= $closeTag; } elsif ($inIGNORE && (($beginTag =~ /<([^>\s])/o) || ($closeTag=~ /<\/([^>\s]+)>/o))) { # seeing tags within ignored fields, ignore them. $ignoredTxt .= $beginTag if ($beginTag); $ignoredTxt .= $closeTag if ($closeTag); } elsif ($ignoreTag && ($beginTag=~/<([^>\s]+)/o) && $ignore{$1}) { # seeing new begining tag specified as ignored $inIGNORE=1; print STDERR "finding ignore begin tag: $beginTag\n" if ($debug); $ignoreTagName=$1; $ignoredTxt .= "\n$beginTag"; } elsif ($ignoreTag &&($closeTag=~/<\/([^>\s]+)>/o) && $ignore{$1}) { $inIGNORE=0; print STDERR "Shouldn't print this! something is wrong! $closeTag\n"; $ignoredTxt .= "$closeTag\n"; } elsif ($removeTag && $closeTag) { $skippedTags .=" $closeTag\n"; } elsif ( $removeTag && $beginTag) { $skippedTags .=" $beginTag\n"; } elsif ($closeTag) { $docBody .= "$closeTag\n"; } elsif ($beginTag) { $docBody .= "$beginTag\n"; } else { # in contents! if ($inTITLE) { $docTitle .= "$content\n";

} elsif ($inDOCNO) { $docID = $content; } elsif (!$inIGNORE) { $docBody .= "$content"; } else { $ignoredTxt .= "$content\n"; } } } } open(LOG, ">$opt_database.ignore") || die "can't open log file\n"; print LOG "==== Skipped tags ===\n$skippedTags\n"; print LOG "==== Skipped contents ===\n$ignoredTxt\n"; close (LOG);

You might also like