tech note

perl one-liner: how to convert file or stream to upper or lower case

So you want to convert a file/stream to upper or lower case in Linux? piece of cake, there are thousands of ways to do that. Usually I leverage tr

convert to upper case:

cat filename | tr "[:lower:]" "[:upper:]"

convert to lower case:

cat filename | tr "[:upper:]" "[:lower:]"

I am sort of tired of tr and was looking for way to use perl, here is my hack:

convert to upper case:

perl -wpl -e 's/^.*$/\U$&/g;' filename

convert to lower case:

perl -wpl -e 's/^.*$/\U$&/g;' filename

It basically match anything (*) in between the beginning (^) and the end ($), substitute (/s) just the same string by applying the modifier, \L (lower case) and \U (upper case). Not necessarily easier than using tr, just an attempt with perl oneliner. Remember TMTOWTDI? Yeah, "There's more than one way to do it".

Where is my drupal login page?

I constantly forget drupal login url, (by default, unless you have built custom aliases) /admin will simply shows a 403 (access denied) and /login returns a 404 (page not found), which is great from the security perspective.

The following are its login urls:
/user
/?q=user
/?q=user/login

For personal blogs and small company sites, a good security practice is to grant access to a group of whitelisted users and deny the rest. In case you don't know, you can easily achieve this task in drupal, administer-> user management -> access rules. Below is my access rules, I only allow myself and blocks anyone else, which is represented as a percentage sign (%).

drupal access rules

Introducing Wufoo forms

Everyone is being hit by the poor economy and everyone is looking for new ways to increase revenue. Recently, I have received a number of requests with almost the same requirements:

Hey, I see an opportunity to do xyz, and all I need is something simple on the web to establish a trustworthy online identity. The site only needs to be simple, easy to use and has something like a contact form so my clients can get in touch with me, I don't need a database or something sophisticated because I understand it will be expensive.

How to:print output with desired formats in awk

I was leveraging the jQuery auto-complete plugin today and my client asked me if I can store all the major cities in the United States. I managed to find a list through wiki, however, it is a HTML table with 273 rows surrounded with HTML tags, my goal here is to get entire city columns, join with single quotes and a comma, then apply them to the auto-complete plugin.

Input:

Rank City State Population
1 New York New York 8,363,710
2 Los Angeles California 3,833,995

Desire Output:
'New York',
'Los Angeles'

Here is my approach:

wget en.wikipedia.org/wiki/List_of_United_States_cities_by_population
awk -v s="'" -v e="'," '{print s $2 e}' filename

The v argument gives you the ability to define simple variable and creates flexibility for automation.

Want to learn more about awk, read this awesome tutorial.

I later discovered an even better approach by relying on Jquery. I first include jquery by using an user script by joanpiedra

Refresh the wiki page to get Jquery included in this page, then I use the following JavaScript in Firebug to populate the data:

$("#sortable_table_id_0 tr td:nth-$("table tr td:nth-child(2)").each(function(){
   var t = $(this).text();
   console.log(t); 
});

Wow, all data gets populated in the firebug console. The rest will be simple copy and paste.

Update:

If you are a mootools fan, you can achieve the same goal with the same code, as long as you have injected mootools into the page, as introduced below:

$('#sortable_table_id_0 td:nth-child(2)').each(function(){
   var t = $(this).text();
   console.log(t); 
});

You will also need to inject mootools into this page, the following script, a slight modification base upon joanpiedra's user script:

// ==UserScript==
// @name          Mootools
// @namespace     http://www.joanpiedra.com/jquery/greasemonkey
// @description	  Play nicely with Mootools and Greasemonkey
// @author        Joan Piedra
// @homepage      http://www.joanpiedra.com/jquery/greasemonkey
// @include       *
// ==/UserScript==


// Add mootools
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://www.mootools.net/download/get/mootools-1.2.3-core-nc.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
^ Top of Page