Friday, January 23, 2009

Regular Expressions in MySQL

Do you need to limit your SQL results and LIKE '%foo%' isn't giving you a narrow enough result list?

Well, I'm sure you know how to narrow down those SQL results with a regex! So all there is to it is the REGEXP operator! Yay!

Example:

SELECT * FROM table WHERE column REGEXP 'data[0-9]';


You get the point; now enjoy! Cheers!

Friday, January 16, 2009

MySQL Search and Replace

I recently had to update some information in MySQL. Yes, I'm sure you're thinking, "yea, so...that's easy!". But, the way the data was entered in the column was more like an entire paragraph! The information I needed to change was only a last name in the paragraph. So, obviously I didn't want to type out the entire paragraph again with my edits just so i can UPDATE the table. So instead, MySQL has a nice replace function built in. Yay!

The code will look as follows:
UPDATE your_table set your_column=replace(your_column,'Smith','Jones');

And now your_column will replace all occurrences of "Smith" with "Jones".

Cheers!

Wednesday, January 14, 2009

Hello, Android SDK


So in an attempt to stay on budget and yet keep myself entertained through this recession, I finally set up my Android SDK on one of my computers. I also finished up my first app! Ok, ok, so it's the generic "hello world" app that all newbies start off with, but hey I still haven't decided what I'd like to build. It's always just fun to tinker around a system anyway.
Let the fun begin!

Cheers!

Tuesday, January 13, 2009

Where's My Ubuntu Screensaver Admin?!

I think this will probably be more of a reminder for me, but in case anyone else has this curious issue with Ubuntu Gutsy Gibbon, then read on!

For whatever reason, I do not have "Screensaver" listed under System>Preferences. I'm using XScreenSaver since gnome-screensaver seemed to just freeze after any amount of time. Anyhow, if you need to configure your settings for XScreenSaver all you need to do is type in xscreensaver-demo in a shell. Tada!

Sheesh, I had to google that every time and then follow it up with a "doh!" once I read what to do!

Cheers!

Tuesday, January 6, 2009

It's 2009 and I Am Back!

Well, it has been over two months since my last post. I have been unfortunately out of ideas on what to blog about. So, perhaps I will slowly stray away from my usual format of blogging about technical recipes and code to bantering on about anything random. Hmmm...we'll see.

Anyhow, happy 2009! I was given an awesome present of a [t-mobile] G1. Yay! I've only had it for a little bit over a week now, but it is awesome. I've managed to get root thus far; but stay tuned for more updates.

Friday, October 24, 2008

Using a Script to Edit a conf File

Well, because I have been uber busy here at work, I haven't been able to write any blog posts. But, since I've been super productive at work, I get to write up a few posts about some more tools I've written to add to my arsenal. w00t!

So my latest task involved editing some of our web server's Apache confs. It consisted of adding a new alias so that we can call in some new app someone else wrote. Okay cool. Alias? Sure not a problem. Er...wait! The alias requires the site's directory name as part of the path! Oh no! That means I can't create one global alias!

Ah, see that's when I thought of writing this script that will look for a specific line in the vhost entries and will enter the new line after it. Pretty nifty eh? :)

So basically, what we have here is a list of all sites in a text file. Now we can use the site's directory name read in as a variable. It will then use awk to search for a specific line, and then print the new line you need right below it!

So if you take a look at the code, I want to add this line:
"Alias /newalias /mnt/www/$site" (where $site is the site directory name variable)
right after each line that fits the regex that searches for any line that has "Alias" and "reports" in one line followed by the site dir name at the end of the line. Obviously if you want to utilize this script you should edit the regex to your needs.

And that's it! Now think of all of the time saved rather than adding a new line manually to EACH vhost entry on EACH server! whoa....scary...


#!/bin/bash

exec < sites.txt
while read site ; do
echo "processing $site..."
awk '{print $0} '"/Alias.+reports.+\/$site"$/'{print " Alias /newalias /mnt/www/'"$site"'"}' httpd.conf > newhttpd.conf && mv newhttpd.conf httpd.conf
echo "done!"
done

Thursday, October 2, 2008

Shell Scripting to Read Data From a File

Okay, so here's your problem: you have a file that contains data that you'd like to parse through a script. It's a lot of data and you don't want to sit there and continuously do the same thing over and over!

The data can be set up as one long column of data or multiple columns of data. In this case for simplicity I'll only use one column of data.

So let's say your data file is as follows:

dog
cat
cow
...
...
(long list of data)


These represent parts of filenames that you need to clear out. You don't want to sit there typing cat /dev/null > dog
cat /dev/null > cat
...
So you come up with a master plan to script the process. The script will be short, sweet, and absolutely time saving!

You first need to read in the file; so this line does the work for you: exec < files.txt. Obviously, in this example my "files.txt" is the filename that contains the data I want to read. After you've opened up the file for reading, you can now parse through the file with a while loop:
while read file ; do
[do some work here]
done


Now note, "file" is the variable that we're assigning to the column. You can call the variable whatever you want. Also remember that I am being simple with this example and only have one column of data. If I wanted to do multiple columns of data, I would just assign more variables to each column. e.g. while read col01 col02 col03 ; do where $col01, $col02, and $col03 will be my new variables.

That's it! I'll leave the customization to you. Cheers!

Here is the full script example:
#!/usr/bin/bash

exec < files.txt

while read file ; do
if [ -s /mnt/$file ] ; then
cat /dev/null > /mnt/www/vdir/z/ec/$file
else
echo "/mnt/$file already empty!"
fi
done