Paragraph grep with perl

Here is a very quick and easy perl one-liner that I use fairly often. When I am working with data that is contextualized in chunks of paragraphs (such as Cassandra columnfamily info), I often want to quickly find a single paragraph for a specific piece of information. Here is how I go about doing that in perl:

my_output_command | perl -00 -ne 'm/ninja/ && print'

This results in any paragraph containing the word “ninja” to be printed. The way this works is by the magic of the -00 flag. This flag causes the perl line-separator variable($/) to be set to ‘\n\n’, resulting in the one-liner iterating over STDIN for each paragraph, instead of each line.

The train is departing in…

It has been a crazy few months for me. I moved half-way across the country to start my new job at reddit. The dust is just now settling, and I finally have a permanent residence nailed down. I should be back to blogging more regularly soon.

One of the benefits of my relocation is that I finally have a viable public transportation option for my commute, namely BART. I have been trying to figure out the train schedule, but I quickly learned the BART timetable is really more of an ideal than concrete fact. The one resource which has been useful is the real time departure info available on the BART website.

Being the typical admin that I am, I decided to replace the act of visiting this site each morning with a very small shell script which audibly informs me of the impending train departures from my local station. In a further demonstration of standard sysadmin behaviour, I threw it together in about five minutes and it is ugly as sin… but it works.

(Note to prospective stalkers, I changed the link to hide my true departing station)

curl '' | \
grep SFO | \
perl -ne '`espeak "The train is departing in "`; while ($x = m/(\d+) min/g) { `espeak "$1 minutes,"` }'

Apparently BART has a full-fledged API for pulling this type of stuff down, but this works for now.

I’ll leave the details of how it works as an exercise for the reader. It should work on any of the stations listed on the departing info page.

Happy trails!

Busy month!

Hey everyone!

Been a very busy month for me. I spent a lot of time for one of my RHCA exams (which I passed!), and a lot more time on various projects at work. I’ll be back to posting regular on a more regular schedule soon. Send me a comment if there is any sysadmin topic, utility, or how-to that you would like to see me post on.


Assuming a new group without relogging

If you’ve ever added yourself to a POSIX group, you’ll notice that your group membership doesn’t change until you restart your current session. The reason being is that your group information is set at your initial login, and all child processes and virtual terminals under that login will maintain those initial groups.

Unfortunately there is no way (that I know of) to reload your group information for your parent session. However, it is possible to assume your new group membership using the newgrp command:

newgrp - nameofgroup

This command will intialize a new session and your user will now belong to the requested group. Please note that this command does not work if your group membership has not yet been set in /etc/group.

Time saving tip: Automatically ls when changing directories

If you spend a lot of time in the shell, you may notice a certain pattern when hopping around the filesystem. You “cd” to where you want to go, then immediately “ls” to see what is in that directory. I found myself doing this extremely often, so I decided to cut my typing time down by automatically listing as I cd.
Read more

Using SystemTap to monitor files being opened

SystemTap allows you to take a real-time peek into any function call in the kernel. To demonstrate this, I wrote an stap script which will print the full path of any files opened by a specific user.
Read more

Creating re-usable SystemTap modules

I have been heavily delving into SystemTap lately. One of the draw-backs most people run into is having to install the kernel devel, kernel debugging, and gcc stuff on any system you want run your SystemTap scripts. You can get around this by building the SystemTap module once, and re-using it on any system with the same kernel.
Read more

The blight of multi-CD installs

I installed Centos 5 on a VM this morning using CD images. By default, the install requested CDs 1, 2, 3, 4, and 5. I removed every package group I could find other than base, and the install informed me that I would need CDs 1, and 5. The install progressed, and the progress bar reached the very end before it requested CD 5. I reluctantly attached the ISO, anticipating that I was about to witness something excruciatingly disappointing. The 5th CD was only used to install a single library package. Really? Really?

Installing the RedHat Directory Server IDM in Ubuntu

Here is quick guide on how to easily install the DS management console on Ubuntu using alien. This has worked on every release of Ubuntu I have tried.
Read more

Re-mapping CapsLock on Windows, Linux, and OSX

I consider myself a wholehearted proponent of the righteous war against the nefarious CapsLock key. Here is how you can go about fighting the good fight on your local turf; be it Windows, OSX, or Linux.
Read more