nix, shell, perl, php, mysql and mac os x tips and tricks

Monday, July 18, 2011

Remove multiple instances of a character (or a pattern) from a URL, then do a 301 redirect, with mod_rewrite

There was actually some html tags appearing in some of my URLs. This hack strips tags and does a search-engine-friendly redirect.
RewriteCond %{REQUEST_URI} ^(.*)<.*?>(.*)$
RewriteRule ^.*$ %1%2 [E=replacer:%1%2]
RewriteCond %{ENV:replacer} !^$
RewriteCond %{ENV:replacer} !^.*<.*?>.*$
RewriteRule ^.*$ %{ENV:replacer} [R=301,L]

This part matches the tags: "<.*?>". Replace that with your character or pattern.

Thursday, July 14, 2011

Check line endings of a file from command line

type 'od -c [filename]' and look for either \n (indicating LF) or \r \n (indicating CRLF)

Search bash history

Not sure why I never new this, but hitting ^r in bash will put you into history search mode. Type the beginning of the command, then just keep hitting ^R to go back in history. Helps when you want to find a long command that you entered previously.

Friday, July 1, 2011

grep pattern to match e-mail address

grep -Eiorh '([[:alnum:]_.]+@[[:alnum:]_]+?\.[[:alpha:].]{2,6})' file.txt