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

Thursday, December 7, 2006

Get files named within a numerical range with curl

This used to work for getting all the This American Life Episodes. May have other uses now.
curl --limit-rate 20240 -O -A 'iTunes/4.7 (Macintosh; N; PPC)' http://wbez-tal.streamguys.us:8020/content/[1-321].mp3

Tuesday, December 5, 2006

Use javascript to search-and-replace international characters in form entry fields (and do some other stuff)

 function ak_hi_check() {

    if  ( (mainForm.shipping_state.value == "AK") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "HI") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "PR") && (mainForm.ShipVia.value != "27") ) {
        alert("You must choose Puerto Rico as your shipping method");
        return false;
    } 
  
else {
        return true;
    }
}

// searches and replaces intl characters
function mySubmit()
 {
     //replace any special characters entered into text entry fields
    document.mainForm.billing_name.value = convert(document.mainForm.billing_name.value);
    document.mainForm.billing_first_name.value = convert(document.mainForm.billing_first_name.value);
    document.mainForm.billing_last_name.value = convert(document.mainForm.billing_last_name.value);
    document.mainForm.billing_addr1.value = convert(document.mainForm.billing_addr1.value);
    document.mainForm.billing_addr2.value = convert(document.mainForm.billing_addr2.value);
    document.mainForm.billing_city.value = convert(document.mainForm.billing_city.value);
    document.mainForm.billing_zip.value = convert(document.mainForm.billing_zip.value);
    document.mainForm.billing_home_phone.value = convert(document.mainForm.billing_home_phone.value);
    document.mainForm.shipping_name.value = convert(document.mainForm.shipping_name.value);
    document.mainForm.shipping_first_name.value = convert(document.mainForm.shipping_first_name.value);
    document.mainForm.shipping_last_name.value = convert(document.mainForm.shipping_last_name.value);
    document.mainForm.shipping_addr1.value = convert(document.mainForm.shipping_addr1.value);
    document.mainForm.shipping_addr2.value = convert(document.mainForm.shipping_addr2.value);
    document.mainForm.shipping_city.value = convert(document.mainForm.shipping_city.value);
    document.mainForm.shipping_zip.value = convert(document.mainForm.shipping_zip.value);
    document.mainForm.shipping_home_phone.value = convert(document.mainForm.shipping_home_phone.value);
    document.mainForm.po_number.value = convert(document.mainForm.po_number.value);
   
    // make sure they are choosing the correct ship method
    if  (ak_hi_check()==false) {
           return false;
    } 
    else {
        document.mainForm.action = "InvoicePreview";
        document.mainForm.submit();
    }
}

// the characters to search-and-replace
function convert(text)
{
    text = text.replace(/À/gi,"a");
    text = text.replace(/Á/gi,"a");
    text = text.replace(/Â/gi,"a");
    text = text.replace(/Ã/gi,"a");
    text = text.replace(/Ä/gi,"a");
    text = text.replace(/Å/gi,"a");
    text = text.replace(/Æ/gi,"ae");
    text = text.replace(/Ç/gi,"c");
    text = text.replace(/È/gi,"e");
    text = text.replace(/É/gi,"e");
    text = text.replace(/Ê/gi,"e");
    text = text.replace(/Ë/gi,"e");
    text = text.replace(/Ì/gi,"i");
    text = text.replace(/Í/gi,"i");
    text = text.replace(/Î/gi,"i");
    text = text.replace(/Ï/gi,"i");
    text = text.replace(/Ð/gi,"eth");
    text = text.replace(/Ñ/gi,"n");
    text = text.replace(/Ò/gi,"o");
    text = text.replace(/Ó/gi,"o");
    text = text.replace(/Ô/gi,"o");
    text = text.replace(/Õ/gi,"o");
    text = text.replace(/Ö/gi,"o");
    text = text.replace(/Ø/gi,"o");
    text = text.replace(/Ù/gi,"u");
    text = text.replace(/Ú/gi,"u");
    text = text.replace(/Û/gi,"u");
    text = text.replace(/Ü/gi,"u");
    text = text.replace(/Ý/gi,"y");
    text = text.replace(/Þ/gi,"THORN");
    text = text.replace(/ß/gi,"s");
    text = text.replace(/à/gi,"a");
    text = text.replace(/á/gi,"a");
    text = text.replace(/â/gi,"a");
    text = text.replace(/ã/gi,"a");
    text = text.replace(/ä/gi,"a");
    text = text.replace(/å/gi,"a");
    text = text.replace(/æ/gi,"ae");
    text = text.replace(/ç/gi,"c");
    text = text.replace(/è/gi,"e");
    text = text.replace(/é/gi,"e");
    text = text.replace(/ê/gi,"e");
    text = text.replace(/ë/gi,"e");
    text = text.replace(/ì/gi,"i");
    text = text.replace(/í/gi,"i");
    text = text.replace(/î/gi,"i");
    text = text.replace(/ï/gi,"i");
    text = text.replace(/ð/gi,"eth");
    text = text.replace(/ñ/gi,"n");
    text = text.replace(/ò/gi,"o");
    text = text.replace(/ó/gi,"o");
    text = text.replace(/ô/gi,"o");
    text = text.replace(/õ/gi,"o");
    text = text.replace(/ö/gi,"o");
    text = text.replace(/ø/gi,"o");
    text = text.replace(/ù/gi,"u");
    text = text.replace(/ú/gi,"u");
    text = text.replace(/û/gi,"u");
    text = text.replace(/ü/gi,"u");
    text = text.replace(/ý/gi,"y");
    text = text.replace(/þ/gi,"thorn");
    text = text.replace(/ÿ/gi,"y");
    return text;
}

Friday, December 1, 2006

Another workaround for system argument list too long error when using grep

find . -type f -maxdepth 1 |xargs grep -rHn [searchword]

Thursday, November 16, 2006

Check uptime in a continuous fashion

while(true) ; do uptime; sleep 3; done

Wednesday, September 13, 2006

This might also work for forwarding e-mail

In the ~user HOME directory, add a file called: .forward
and put inside it this line:
forward@email.com
Then set the permissions to 600.

Making mail aliases at the server level

You can create an alias in /etc/aliases (or /etc/mail/aliases) on
the server like this:
aliasuser: forward@email.com
Then run 'newaliases'.

Tuesday, June 6, 2006

Import text into MySQL from command line

mysqlimport --fields-terminated-by=, --lines-terminated-by="\n" --user=user--password DATABASE TABLE_NAME.csv

Monday, April 24, 2006

ms sql for search-and-replace based on key/value table (not really tested)

    DECLARE @word1 varchar(1000)
     DECLARE @word2 varchar(1000)
     DECLARE word_cursor CURSOR LOCAL fast_forward FOR
     SELECT old_word, new_word FROM word_replace_table

     OPEN word_cursor

     FETCH NEXT FROM word_cursor INTO @word1, @word2

 WHILE @@FETCH_STATUS = 0

 BEGIN


   SET xact_abort ON
   BEGIN tran

   DECLARE @otxt varchar(1000)

   SET @otxt = @word1 /****/
   DECLARE @ntxt varchar(1000)

   SET @ntxt = @word2 /****/

   DECLARE @txtlen int
   SET @txtlen = len(@otxt)

   DECLARE @ptr BINARY(16)
   DECLARE @pos int
   DECLARE @id int
   DECLARE curs CURSOR LOCAL fast_forward
   FOR
   SELECT

   productId,
   textptr(description),
   charindex(@otxt, description)-1
   FROM
   product
   WHERE
   description
   LIKE
   '%' + @otxt +'%'

   OPEN curs

   FETCH NEXT FROM curs INTO @id, @ptr, @pos

   WHILE @@fetch_status = 0

   BEGIN

   print 'Text found in row id=' + cast(@id AS varchar) + ' at pos=' + cast(@pos AS varchar)

   updatetext product.description @ptr @pos @txtlen @ntxt

   FETCH NEXT FROM curs INTO @id, @ptr, @pos

   END

   CLOSE curs

   DEALLOCATE curs

   commit tran

     FETCH NEXT FROM word_cursor INTO @word1, @word2
 END

     CLOSE word_cursor
     DEALLOCATE word_cursor

Friday, April 7, 2006

clean ctrl-M chars from a file (be VERY careful with this one - better to test)

cat options.csv | sed "s/[^M]$//" > options1.csv

Tuesday, April 4, 2006

Shell script to make all files in directory lowercase

#!/bin/bash
for i in $(ls); do
    oldname="$i"
    newname=$(echo "$oldname" | tr 'A-Z' 'a-z')
    if [ "$oldname" != "$newname" ]
        then
            mv -i "$oldname" "$newname"
        fi
done

Thursday, March 30, 2006

Search and replace in MSSQL (specifically in TEXT field)

Just use Ctrl+Shft+M in Query Analyzer to replace the parameters. Then hit "play" in query analyzer til u don't get any more hits.
/*
*
* Search & Replace
*
* Use Ctrl+Shift+M to replace template values
*
*/

set xact_abort on
begin tran

declare @otxt varchar(1000)
set @otxt = ''

declare @ntxt varchar(1000)
set @ntxt = ''

declare @txtlen int
set @txtlen = len(@otxt)

declare @ptr binary(16)
declare @pos int
declare @id int

declare curs cursor local fast_forward
for
select
 id,
 textptr(),
 charindex(@otxt, )-1
from
 
where
 
like
 '%' + @otxt +'%'

open curs

fetch next from curs into @id, @ptr, @pos

while @@fetch_status = 0
begin
 print 'Text found in row id=' + cast(@id as varchar) + ' at pos=' + cast(@pos as varchar)
 
 updatetext  . @ptr @pos @txtlen @ntxt

 fetch next from curs into @id, @ptr, @pos 
end

close curs
deallocate curs

commit tran

Wednesday, March 1, 2006

Workaround for "argument list too long" error on 'nix

find /home/rory/backup/Mail/outbox/ -type f -name '*' -exec cp {} /home/rory/Mail/outbox/ \;

Monday, February 27, 2006

List only directories

ls -d */

Delete files older than x days

find /home/mysite/dir/ -type f -mtime +35 -exec rm {} \;