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);