Tuesday, June 29, 2010

PERL: Removing Text Within Parentheses

I recently wanted to get a list of major landmarks, but the text list had the name of the landmark, followed by the location of the landmark in parentheses: ex. Eiffel Tower (Paris, France). I just wanted the names of the landmarks without the text in the parentheses, so had to figure out the command to remove all the parenthesized text.

Perl was the winner as tool of choice. There was a small trick to doing this seemingly trivial task, so document it we shall.

Original: Eiffel Tower (Paris, France)
Desired: Eiffel Tower

There are two ways to do it based on what you want:
perl -p -e 's#\(.*\)##g' textfile
You may have seen 's/oldtext/newtext/g' as the syntax before and are wondering why I am using hash marks (or pound signs) instead. You don't have to use the forward slash, it is just the common way, but if you want to use the forward slash in the search text without having to escape, using hash marks are the way. It can also be used to make it easier to read. Now, onto the command--the \( obviously says look for a left parenthesis, then there is the critical .* which says find any number of any characters. Finally, we close it off with a right parenthesis. This will find anything encapsulated by two parentheses.
perl -p -e 's#\([^)]*\)##g' textfile
This solution will also do the same thing based on our Original text string, but it is slightly different. The [^)] is telling "any character that is not a right parenthesis." The carat (^) is negating everything in the brackets. This is useful if you are making an exclusion set. You can place several characters, [^$)?], and it will look for any character except a $, ), or ?.

Since the two commands work the same for the given example, let's show how the commands will vary in different situations:
If textfile contains
1. Paris (France,) Hilton (Hotel)
2. Paris (France (Hilton) Hotel)
Using
perl -p -e 's#\(.*\)##g' textfile
The results would be:
1. Paris
2. Paris
Note the danger here is that, even though in line 1 Hilton is not in parentheses, it gets removed because there is an ending right parenthesis at the end of the line. This may not be the expected/intended operation.

Next, using
perl -p -e 's#\([^)]*\)##g' textfile
The results would be:
1. Paris Hilton
2. Paris Hotel)
The operation for line 1 may have been what we were expecting, but line 2 doesn't look good. The moral here is to understand what you are trying to do and choose the correct command to do the appropriate operation.

Thursday, June 24, 2010

HEALTH: Quasar Workouts

My new favorite method of exercise is Quasar Training. It is a method my friend and I made up recently in the gym to help "spice up" our gym routine. I can't say that the idea is novel--I think most everything has been thought of already--but it is new for us. We called it Quasar because it sounds awesome and motivating, but it really is QSSR training.

Quick Start, Slow Release (QSSR), aka Quasar, is a technique that follows exactly what the name implies. Start the exercise with a Quick Start effort, working the Fast Twitch muscles for speed increases. At the peak of the movement, right before you begin the return movement, you change over from "Quick" to "Slow". The Slow Release is returning in a very slow motion, about 2 "Mississippi Counts" worth.

In one exercise, you are increasing your explosive speed, and gaining strength. The muscle strength you gain is also being exercised differently. Since the return is often a quick release of muscle tension, most workouts rarely exercise the muscles in this fashion. Slow Release will allow quicker gains and a more complete muscular workout as you employ stabilizer muscles as well.

A great way to experience Quasar Training immediately is to a set of Quasar Push-Ups right now. Lay on the floor in Push-Up position, but with your chest on the floor. Launch yourself up as fast you can (Quick Start) and then lower yourself back down very slowly (Slow Release). Repeat for a complete set. Do not let your chest touch the ground between repetitions. Each time you are pushing up as fast as you can to build explosive speed, then you are returning down, fighting gravity very slowly.

This has been talked about on EzineArticles as well: http://ezinearticles.com/?Quasar-Workouts-Are-the-Brightest-Way-to-Stellar-Physical-Fitness&id=4523423