Script examples
I rarely need to knock up a perl of shell script. But when I do, I find I've forgotten how to do this or that. I can remember though that I did it before, so this page really just allows me to find some old scripts and copy my bad perl from yesteryear to a new, and just as bad, script.
If you want to write bad perl, have a look at some of the examples below.
Perl
| perl bits | ||
| striphtml.pl | You've got a load of static webpages with an old template on that need a new look. You obviously want to keep the content, title, heading etc of the pages, just remove the look, and either strip it down to basic html or replace it with a new look. That's what I had to do, and this script does this. | |
| addcode.pl | add some text (from a text file) to the top of a file | |
| changeurls.pl | will read in a text file of strings and passed a list of filenames (e.g. *.html) on the command line, will then look find and replace each string for each file, it current form, once it finds a matching string it will then does a simple regexp to change some characters within it. | |
| regexp.pl | I hate regular expressions, this does some simple mattern matching, given a line of a log, it will grab the date and a reference number that appears somewhere in the line. | |
| mailsize | [in progress] this script will report on the size on your inbox, and show the five largest emails within it. It's based on the idea that often a large proportion of a mailbox's size is taken by a few emails with large attachments, this highlights these to the user. | |
| getwebpage.pl | Very simple script that fetches the contents of a webpage and uses a regular expression to grab out a specific bit of information. In this example we had a very long list of URLs that each linked to a page for an online electronic journal. We wanted to find the journal title that each URL linked to. By looking at the html of one page, we can see where the journal title appears, and from this, we can create a pattern to match based on the standard html that appears on either side of the title. | get urls and webpages LWP::Simple |
| removeattachments.pl | This was written in a rush at a time where I hadn't used perl for a while. It reads in a 'mbox' standard email folder file, and removes, in the crudest way possible, all email attachments from the file. | |
| newtemplate.pl | This was written to change the look of a large number of webpages. Most of the pages had similar formatting regarding the way the old look was applied, however such things as the main heading on a page varified greatly (H1, H2, b, etc). The new template is stored in a number of text files in the same dir as the script. The script grabs certain elements out of the old page, such as title, keywords metatag, page heading, and of course the content, and then merges these with the new look template. | |
| stat.pl | produces usage statistics for the ezproxy utility. See this page for more infomation. |
Shell Scripts
| bulkrename.sh | Rename a set of files on bulk | |
| movejunk.sh | creates a .forward file, if a 'junk' or 'Junk' email folder exists | |
| replace.sh | go through a set of files and replace one string with another (using sed) | |
| striphtml.sh | Looks like an early attempt at a script by me :) I seem to remember that we had a number of html files which contained a huge number of links, we wanted to extract those links to put into a database (basically converting some static pages in to a database driven thing). | |
| rotate.sh | simple script to rotate a log file, lookup ip addresses and gzip up the old log, which is named as today'd date, i.e. yymmdd.log.gz |