Click the box titles below to expand:

How to's

XSLT

GOOGLE MAPS API

PHP

Antelope

Perl

Document Object Model (DOM)

UNIX

Generic Mapping Tools (GMT)

Miscellaneous

Projects

Courses Taught

Latest Favorites

Mac OS X

Web Development

Beta

How To :: Snippets of useful UNIX code that make my life easier

Find and replace from the command line using Perl

A little command line call to Perl to replace all instances of the PHP function set_include_path() with a comment '//'. Nice and easy search through a whole series of directories.

perl -pi -e 's/set_include_path\( \"\/export\/home\/apache\/htdocs\/eqinfo\/incl\/\" \) ;/\/\//g;' */*/index.php

Test for Antelope version using TCSH

This statement goes in your .tcshrc file:

if( -r /path/to/antelope/4.10/setup.csh ) then
    source /path/to/antelope/4.10/setup.csh
else
    source /path/to/antelope/4.9/setup.csh
endif

Modify permissions on an RCS file

Go into the RCS directory (if there is one) and chmod on the ,v file. For example, you have a file called documents.html that has read-only permissions. You want it to have executable permissions. You need to go into the RCS directory and chmod the documents.html,v file to +x.

Repeat the last part of a UNIX command

Repeat the last argument of the command with a new command.

[user@host]$ mkdir this
[user@host]$ cd !$
cd this

Using find and grep together

Search for string using find and grep together:

find ./ -exec grep "searchStr" \{\} \; -print

This prints out all matching results with the file name on the next line.

Getting MySQL query results into a text file in your home directory

This is actually really simple... there are two methods...

Method 1

  1. In the typical MySQL environment, query results are pushed to STDOUT (ie. the MySQL command line prompt). You need to change this to cat and then point cat to your text file in your home directory.
  2. Run your query.
  3. Exit mysql and you will see your result query in the text file you defined with the pager option. To return the output to STDOUT just type nopager at the MySQL prompt.
mysql> pager cat > /path/to/test.sql
PAGER set to cat >  /path/to/test.sql

mysql> SELECT email FROM members WHERE password='' AND email != 'null';
632 rows in set (0.01 sec)

mysql> nopager
PAGER set to stdout

Method 2

Make the database do the work, and just add the INTO OUTFILE within your SELECT statement:

mysql> SELECT email FROM members WHERE password='' AND email != 'null' INTO OUTFILE '/tmp/test.sql';
Query OK, 632 rows affected (0.04 sec)

Using cat in fancy ways

To create a file with 'cat', type cat and the redirection character ">"
(greater-than) and a file name.  Everything from Standard In (STDIN)
eg. everything you type (or paste in a copy/paste operation) then gets
written to the file until a CTRL-D is typed. The file will be created
if it does not already exist or overwritten if it already exists. If
you use double greater-than as in "cat >>" and a file-name the file
will be appended to rather than overwritten.

Some examples:

cat > newwork.text (then type whatever you want in the
file and finish with a CTRL-D) write everything that is typed following
the 'cat > newwork.text' command, up to but not including the CTRL-D,
in to the file 'newwork.text' Either create a new file 'newwork.text'
or overwrite it if one exists.

cat >> addwork.text (then type whatever
you want in the file and finish with a CTRL-D) Append everything that
is typed following the 'cat > addwork.text' command, up to but not
including the CTRL-D, to the end of file 'addwork.text' Append to an
existing file 'addwork.text' if it exists. Create it if it does not
already exist.

cat > screen.text (then highlight a piece of screen
text with the left button and write it back with the middle button)
CTRL-D copy everything in the highlighted section of screen text to the
file screen.text. Either create a new file 'screen.text' or overwrite
it if one exists.

cat >> screen.text (then highlight a piece of screen
text with the left button and write it back with the middle button)
CTRL-D copy everything in the highlighted section of screen text and
append to the end of the existing file screen.text. If it does not
already exist, create it.

cat filea fileb filec > combo-file Copy everything from files 'filea',
'fileb', 'filec' in to 'combo-file' Either create a new file
'combo-file' or overwrite it if it already exists.

cat filea fileb filec >> combo-file Copy everything from files 'filea',
'fileb', 'filec' and append to the end of the existing file
'combo-file'. If 'combo-file' does not exist, create it.

cat /dev/null > empty-file Create an empty file called 'empty-file. If
a file by that name already exists, empty it out.  (Note: '/dev/null'
is a special null device on UNIX systems.)

Source: http://serv1.scnc.k12.mi.us/howto/edit/cat.html

Convert KML to GMT-style xy files

Go to this page

Cool Vim regex

(1) Replace all occurrences of the tag <latitude> followed by a two digit integer, decimal place, then three decimal places, with the same thing, but appending a closing <latitude> tag:

:%s/<latitude>[0-9]\{2}\.[0-9]\{3}/\0<\/latitude>/g

For the replacement part of the search and replace:

#Meaning#Meaning
&the whole matched pattern\Lthe following characters are made lowercase
\0the whole matched pattern\Uthe following characters are made uppercase
\1the matched pattern in the first pair of \(\)\Eend of \U and \L
\2the matched pattern in the second pair of \(\)\eend of \U and \L
......\rsplit line in two at this point
\9the matched pattern in the ninth pair of \(\)\lnext character made lowercase
~the previous substitute string\unext character made uppercase

You can group parts of the pattern expression enclosing them with "\(" and "\)" and refer to them inside the replacement pattern by their special number \1, \2 ... \9. Typical example is swapping first two words of the line:

s:\(\w\+\)\(\s\+\)\(\w\+\):\3\2\1:

where \1 holds the first word, \2 - any number of spaces or tabs in between and \3 - the second word. How to decide what number holds what pair of \(\) ? - count opening "\(" from the left.

Use tar to copy directories safely across hosts (with SSH)

This little gem allows you to copy a whole directory, and instead of using cp -R (which occasionally screws up). Provided you have your SSH keys organized, you can pipe across system boundaries. Tight.

tar cf - . |ssh remotehost "cd /targ/dir; tar xf -"

Source: http://www.mcnabbs.org/andrew/linux/shellhacks/

Using shell_exec() in PHP and calling binaries that rely on other binaries

A long winded title, but what we are discussing is the ability of the shell_exec() function in PHP to correctly determine the paths for other binaries. This is best illustrated with an example: I use the shell_exec() function to run ImageMagick's convert command on several EPS files to transform them into PNG formatted files. convert uses Ghostscript, and unless you tell PHP where to find Ghostscript, it will result in an error. So, you need to tell PHP about the Ghostscript PATH. The solution? When calling shell_exec( 'convert -trim myfile.eps myfile.png' ) add an 'export PATH="/path/to/binary";' prefix.

<?php
$cmd = 'export PATH="/usr/local/bin/"; convert -trim myfile.eps myfile.png
?>

Source: http://www.php.net/manual/en/function.shell-exec.php#78506

Sort directory on file size

By default the UNIX ls command does not sort by file size. You can create this by passing the results of a ls command to the UNIX sort command:

ls -al | sort -rn +4

Source: http://www.unix.com/unix-dummies-questions-answers/6518-sorting-ls-filesize.html