Monday, December 13, 2010

Delete all files except for some

Sometimes you want to delete all files in a folder, except for a few that match a pattern. Or you want to move them to a different folder. Using ls, awk and xargs, this can easily be accomplished in Linux or any *nix environment.

Let's say you have thousands of files in a folder and you wish to move all the files that are not from Oct2908 or Jul2707 into a folder called 'old'. Here's the one-liner that accomplishes this with ease:

ls | awk '!/Oct2908*/&&!/Jul2707*/' | xargs -i -t mv ./{} old/{}

First we call ls.

Then we pipe its output to awk, which matches all files that do not contain the pattern 'Oct2908*' and do not contain the pattern 'July 2707*'.

In turn, we pipe this to xargs, which calls mv on each file, moving it to the 'old' folder from the current folder.

Easy enough, and can be changed to any operation instead of mv, and any folder instead of 'old'.

Monday, December 6, 2010

How to use a sorted map in Java - TreeMap

Sometimes in Java, you want the ease of using a map, with all the ease that using get() and put() entails. If you'd like the keys to be sorted, you could implement your own Comparator (and you pretty much have to, for complex objects). But most of the time you want a map with simple objects like Integers or Strings in them.

Enter TreeMap to the rescue. This nifty class is part of the Java Collections framework and is a sorted map, where the keys are sorted according to their natural ordering. Simply put, this means that for simple objects like Strings and Integers, you have to do nothing more than declare your map.

Consider the following text in a text file, taken from here:

Overtones of guilt about teenage hormonal episodes aside, this graphic novel holds your attention. The monochromatic format uses its high contrast to draw your eye to the stark emotions on the characters’ faces. The heads are large, often oversized and the usual emotions of angst, betrayal, anger, humiliation, terror, shame, guilt and apathy that make up the pre-pubescent psyche are portrayed realistically here. This is no Fuuuuu comic.

If you have to count the number of times each word occurs in a text file and print out the totals, sorted by the words themselves, this is easy with the following code:

public class WordCount
{
public TreeMap < String, Integer > wordMap = new TreeMap < String, Integer > ();

public static void main(String[] args) throws IOException
{
WordCount w = new WordCount();
w.countWords();
for (Map.Entry<String, Integer> entry : w.wordMap.entrySet())
{
System.out.println(entry.getKey() + " occurred " + entry.getValue() + " times");
}
}
void countWords() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("/tmp/a.txt"));
String line = "";
while((line = br.readLine()) != null)
{
String[] tokens = line.split("\\s+");
for(int i = 0; i < length; i++)
{
int count = wordMap.get(tokens[i]) == null ? 0 : wordMap.get(tokens[i]);
wordMap.put(tokens[i], ++count);
}
}
}

}


Your output, sorted by the words in ascending order, is:



Fuuuuu occurred 1 times
Overtones occurred 1 times
The occurred 2 times
This occurred 1 times
about occurred 1 times
and occurred 2 times
anger, occurred 1 times
angst, occurred 1 times
apathy occurred 1 times
are occurred 2 times
aside, occurred 1 times
attention. occurred 1 times
betrayal, occurred 1 times
characters’ occurred 1 times
comic. occurred 1 times
contrast occurred 1 times
draw occurred 1 times
emotions occurred 2 times
episodes occurred 1 times
eye occurred 1 times
faces. occurred 1 times
format occurred 1 times
graphic occurred 1 times
guilt occurred 2 times
heads occurred 1 times
here. occurred 1 times
high occurred 1 times
holds occurred 1 times
hormonal occurred 1 times
humiliation, occurred 1 times
is occurred 1 times
its occurred 1 times
large, occurred 1 times
make occurred 1 times
monochromatic occurred 1 times
no occurred 1 times
novel occurred 1 times
of occurred 2 times
often occurred 1 times
on occurred 1 times
oversized occurred 1 times
portrayed occurred 1 times
pre-pubescent occurred 1 times
psyche occurred 1 times
realistically occurred 1 times
shame, occurred 1 times
stark occurred 1 times
teenage occurred 1 times
terror, occurred 1 times
that occurred 1 times
the occurred 4 times
this occurred 1 times
to occurred 2 times
up occurred 1 times
uses occurred 1 times
usual occurred 1 times
your occurred 2 times


It should be trivial enough to switch the keys and values around to print the most-occurring words first, instead of sorting by the words. Good luck!

Thursday, November 25, 2010

How to make an Operating System with Powerpoint?

Sometimes Yahoo! Answers has the best questions. Gold, Jerry, gold!

Check this gem out.

How to make a operating system?

how to make a operating system with powerpoint

i have microsoft office 2010 professional full version

And the best answer:

Open a new page, and put the following shapes:

a cylinder, labeled "disk"

a triangle, labeled "printer"

a rectangle, labeled "keyboard"

an oval, labeled "monitor"

a cloud, labeled "internet"

then finally, a circle, labeled "operating system"

Draw lines from the circle to each of the other shapes.

Wednesday, November 24, 2010

strapanzla: Mysterious Shadow People

strapanzla: Mysterious Shadow People: "City View Kumi Yamashita has a secret power. She can place wood or metal objects in just the right light to make mysterious shadow people sh..."

Sunday, November 21, 2010

Create a custom Joomla login form



Joomla is a very powerful CMS and you can tweak almost any aspect of it to your liking. Today we'll talk about making a custom login form for your Joomla website.

The usual way to add a login form to a page is to go into the Joomla backend control panel and add the Login box to a page, so that the user is presented with a login form on that page. But what if you wanted a centralized login form? One that actually looked good and had its very own page? You'll need DirectPHP or some other extension that allows you to embed PHP in Joomla articles to make the following method work.

To do so, we'll do the following:
1. Create a blank article, and note down its id, let's say its id is 44, for this example. Call it "My Login Form"



2. In the Article editor, edit article 44 and create a form with the following code:

<form action="/index.php?option=com_user&amp;lang=en" method="post" name="com-login" id="redirect" name="redir">
    Login: <input name="username" />
    Password: <input name="passwd" />
    <input type="hidden" name="remember" value="yes" />
    <input type="hidden" name="option" value="com_user" />
    <input type="hidden" name="task" value="login" />
    <input type="hidden" name="return" value="aW5kZXgucGhwP29wdGlvbj1jb21fY29udGVudCZ2aWV3PWFydGljbGUmaWQ9OTk=" />
    <input type="hidden" name="<?php echo JUtility::getToken(); ?>" value="1" />
    <input type="submit" name="Submit" />
</form>

 
The important things to note about this form are the hidden variables. The task must be set to login. The option must be set to com_user. The next two hidden variables are more interesting. The return input field's value is a base64-encoded string that represents the URL you wish to redirect the user to after the login. In my Joomla install, my Member Page has an id of 99, which makes the URL "index.php?option=com_content&view=article&id=99". Using the base64 encoder from here, I got the encoded string as "aW5kZXgucGhwP29wdGlvbj1jb21fY29udGVudCZ2aWV3PWFydGljbGUmaWQ9OTk=", which is what you see above. You can read more about the JUtility class at its official documentation page, but suffice it to say that you need to include that hidden variable to prevent spoofing.

3. Finally, modify your template so that the Login link at the top or left points to the id of your custom login form, 44.

We are done. When someone tries to log in now, they'll be taken to your custom login form page, which will process their credentials and redirect them to whatever page you wish them to go to. With DirectPHP or some other PHP extension, you should be able to dynamically change the redirection URL in the return value, but that shouldn't be too difficult.

Remember to not change the form input names, since they must be exactly those for Joomla to process the form. Of course, you'll need to style this custom login form with CSS or your template's styles to fit in with your Joomla look-and-feel, but that should be fairly straightforward.

Saturday, November 20, 2010

Manny's Barbershop in New Westminster



On Sunday, I had the pleasure of discovering a new barber. Now, going to a new barber is a non-trivial event in a man's life. You have to break up with the old one and go to a new one. If the old place you got your haircuts at was some faceless place in the mall, you're in for a relatively guilt-free new-barber experience. Luckily, this was just the case for me. I didn't have to break up with my old barber, I used to get my hair cuts at Magic Cuts in the mall.

In New Westminster, at the corner of Columbia and Sherbrooke, I espied a sign that said "Manny's Barbershop". The sign resembled something out of Forrest Gump. Intrigued, I memorized the number and called it a few days later. Manny answered the phone and when I aske dhim how much he charged for a haircut, told me "$2.50". I couldn't believe my ears and asked him to repeat himself, thinking I might have misheard him. "$2.50", he repeated again over the bustle of shears and conversation and hung up.

I showed up a few hours later and on walking in, Manny greeted me and asked if I was the gentleman who'd called. I nodded and he pointed at the sign that said "$12.50 - Men's haircut" and told me, "See how much I charge for a haircut?". Relieved that the price was more realistic, I sat down and solved a crossword as I waited.

My turn came and I got in the chair. Manny told me that telling new callers the absurdly low price of $2.50 was a ruse he employed to keep stingy people out. The inference was clear: tip or else! He kept up a steady stream of conversation, punctuated by remarks of a very politically incorrect nature from time to time. But let's face it, the audience there was composed of bricklayers, carpenters, a tile man and a welder. Hurly-burly tradesmen who probably relished the occasional blue joke and raunchy discussion. I'm personally not easily offended at all, but I'd recommend staying away from there if you're the sensitive type.

The haircut ended, Manny brushed off the keratinous detritus. I inspected the new do from all angles and was satisfied. A job well done and at $12.50, a real steal. Manny was friendly, courteous and professional. If you're looking for a new barber in the New Westminster area, you could do worse.

Saturday, November 13, 2010

The Templeton on Granville



I was first introduced to The Templeton Diner on Granville in late 2003. It was cool and chic, the retro decor complementing the laid-back feel and general aura of organic somnolence that seemed to pervade the air. The food was good, and the person who took me there (an avid organic food buff) assured me the labels were real and not just pasted on by Pepsi in the parking lot behind the kitchen. I was a fan of the food, and the tables, and the little jukeboxes on every table, along with the plethora of newspapers that one could read as one waited for their order. The long bar with the stools summoned up images of a bartender mopping up the table and listening to your troubles after a hard day at the office.

That was then. Last year began the decline in my expectations of service at the Templeton. Brusquely greeted by the waitress, we were ushered to our table. The food took a while to arrive, but that was excusable. After all, we had wandered in during the lunch hour. All that was missing was the cheerful atmosphere and smiles that had hitherto greeted us on every occasion. We ate, paid our bill and left. Time eased the memory of that borderline-rude lunch and in a few months, we decided to go there again. Minderbinder was enamoured with the place and we decided to give it another shot.

The service this time was abysmal. The quality of service had plummeted like the Hindenberg in its dying throes. We sat at the long bar and ordered our food. It took over fifty minutes for our food to arrive. Yes, you read that correctly, 50! You must be thinking, "Well Yossarian, you're a chump for having stuck around. I would have left aeons ago". The only problem was, we were with a party of seven and the venue we had to visit was right across the street, so we were loath to move. The food finally arrived, in the time it would take entire civilizations to rise and fall on Epsilon IV. Even the quality of food had suffered. Sometimes one is so hungry that when the food arrives, it is consumed ravenously, with nary a thought for taste or texture, with the ingestor seeking only to quench the fires within one's belly and disregarding all gustatory feedback.

Uunfortunately, this was not the case here.We could taste the food and it wasn't great. Not even close to what it had been in the halcyon days of The Templeton's former glory. We gulped it down and asked for the bill. Now began the second episode of waiting, thankfully dwarfed by the first. Finally, we stood up and asked the waitress for our bill, and she snapped at us. She actually told us rudely that she'd told us we could pay at the till, when she'd said no such thing. Oh well, what are seven witnesses against one, especially when the one is such an august personage as a food carrier at the Templeton on Granville. Suitably chastened, we marched along the length of the bar and paid our prandial dues and left.

Thus ended our final sojourn into the dismal cave known as the Templeton, jealously guarded by the harridan with the receipts. As much as we hate writing negative reviews, we feel it our duty to warn the world against the shoddy service, brusque bearing and vastly-inferior victuals now served at The Templeton.