Posted by Nate Murray - Feb 28, 2008
Currently I need to do some bulk transformation of some file names.
I have, say, 3 file names: 2.640-849.0.jpg, g2650ohr.jpg, and k2.26mr.jpg (so these are treated as three separate strings)
I want a (hopefully) single regex to substitute all of the . (periods) with - (dashes) except for the .jpg extension.
A first draft would be
s/\./-/g
but that gives you 2-640-849-0-jpg. Note the extension is transformed as well.
I could then do a second regex like s/-jpg/.jpg/ but that feels less than optimal.
The solution is a zero-width negative look-ahead assertion
man perlre for more information on this, but the basic idea is that
you match occurances of the preceeding expression that ate not followed by the zwnla.
For example /foo(?!bar)/ matches any occurrence of foo that isn’t followed by bar.
Combine this with the common perl utility rename and we get the following:
rename -n 's/\.(?!jpg)/-/g' *
Note that Matz has decided not to include the zero-width negative look-ahead assertion to Ruby.
UPDATE
Thanks goes out to Matt Pulver for submitting these improvements to the above regex:
This will handles cases like test.jpg.jpg
s/\.+(?!jpg$)/-/g
If you want to substitute all non-alpha-numeric characters and support other file extensions try:
s/[^0-9a-z](?!\w+$)/-/g
Posted by Nate Murray - Feb 28, 2008
I’ve used /usr/bin/env for years but I just realized today that it can be very useful for testing processes that are run with cron or monit.
env has a nice option for clearing out all of the environment variables:
-i, --ignore-environment - start with an empty environment
Now you can try to run your script in an environment very similar to cron or monit. Note that this option even clears the PATH so you need to specify full paths or specify a PATH variable.
/usr/bin/env -i HOME=/path/to/my/home /path/to/do_script.sh
Posted by Nate Murray - Feb 26, 2008
Lets say you have a file called REVISION which contains a single number. If you want to increment the number in that file you could run the following command:
Wrap that up as a nice shell script and you get a nice increment command:
[nathan@nate ~]$ cat REVISION
1
[nathan@nate ~]$ increment REVISION
[nathan@nate ~]$ cat REVISION
2
Posted by Matt Pulver - Feb 22, 2008
Ever since I came across ruby’s Enumerable inject function, I have been curious about its applications, and how to best think of it in its most general mathematical form.
[Read more]
Posted by Nate Murray - Feb 21, 2008
Posted by Nate Murray - Feb 17, 2008
Here is an example of a simple way to do circular drop-shadows in Processing.

You can view the interactive demo here.
Read more for the source code.
[Read more]
Posted by Matt Pulver - Feb 15, 2008
In case anyone wants to get djb’s daemontools up and running quickly on an OS that uses Ubuntu’s System V init-replacement upstart, here’s what works for me:
Create a 3-line file /etc/event.d/svscanboot :
That should start svscanboot with each system bootup. To have init start svscanboot without rebooting, do
as root after creating the above file.
Posted by Nate Murray - Feb 12, 2008
In Rails, you sometimes need to to break the MVC pattern. In my case, I wanted to take a fully-generated page and extract a certain portion of it in a helper. In this way I was able to base a foreign-channel’s template on my application’s template.
The key to this black magic is ActionController::Integration:: Session.new. Note that in order to use this class you will need to put the following line in config/environment.rb.
Then from your helper function you can GET a page given a url. In the example below I want to extract the portion of the page between begin #{token} and end #{token}
Posted by Nate Murray - Feb 11, 2008
A very senior Microsoft developer who moved to Google told me that Google works and thinks at a higher level of abstraction than Microsoft. “Google uses Bayesian filtering the way Microsoft uses the if statement,” he said. Reg quoting Joel likening the interview process to bayesian filtering.
Posted by Nate Murray - Feb 9, 2008
Paul Graham has a nice, short, essay on programmers holding a whole project in their head. Its an interesting read and I agree with many of his insights into the programmer’s mind. He also lists helpful tips which, indirectly, give suggestions for creating a productive programming environment.
http://www.paulgraham.com/head.html
Posted by Nate Murray - Feb 9, 2008
Hey guys, here is a new program I have not tried (hence the “claim”). It is supposed to allow you to edit css live in IE on windows:
CSSVista
Edit your CSS code live in Internet Explorer and Firefox
Posted by Nate Murray - Feb 9, 2008
Hey guys, here’s a handy tip for testing XML: assert_xpath. I’m not sure where I found this.
Posted below or at: http://pastie.caboo.se/102945
Posted by Nate Murray - Feb 9, 2008
I stumbled upon a new email client written in ruby that aims to be the “email client of choice for nerds everywhere.” From the project web-page:
http://sup.rubyforge.org/ Sup is a console-based email client for people with a lot of email. It supports tagging, very fast full-text search, automatic contact- list management, and more. If you’re the type of person who treats your email client as an extension of your long-term memory, Sup is for you.
Sup makes it easy to:
Handle massive amounts of email. Mix email from different sources: mbox files (even across different machines), IMAP folders, POP accounts, and Gmail accounts. Instantaneously search over your entire email collection. Search over body text, or use a query language to combine search predicates in any way. Handle multiple accounts. Replying to email sent to a particular account will use the correct SMTP server, signature, and from address. Add custom code to handle certain types of messages or to handle certain types of text within messages. Organize email with user-defined labels, automatically track recent contacts, and much more! The goal of Sup is to become the email client of choice for nerds everywhere.
Posted by Nate Murray - Feb 9, 2008
I just learned a really great svn feature. Its called “svn blame”. You run it on a file and it shows you who is responsible for writing each line. For instance:
Notice that it shows the revision number on the left along with who is responsible for each piece of code.
Posted by Nate Murray - Feb 9, 2008
Some of our code has been slow for a while. Now that we are importing more sites its come to a point where profiling and optimizing is a necessity.
ruby-prof is a great tool for profiling your ruby code. For instance I can do the following:
SITE=mysite ruby-prof -p call_tree -f ~/s3/doc/profile/mysite_importing_call_tree.kcg convert.rb
That will output a call tree file that can be read by another great program: KCachegrind. KCachegrind helps you sort and visualize what is taking up the most time. In the screenshot attached you can see that Kernel::clone takes up 5% of the whole process.
Read more about ruby-prof Read more about KCachegrind
Posted by Nate Murray - Feb 9, 2008
Just learned that moving a physical server to a virtual one is a common task and is called p2v (Physical to Virtual).
Xen comes with a linux P2V program: (see http://www.solutioncentre.co.uk/products.php?productid=100)
Also if the auto-tool doesn’t work here is an article on how to do it manually: http://wiki.xensource.com/xenwiki/XenManualPtoVProcess
Posted by Nate Murray - Feb 9, 2008
Here is a great article with sample code for a skeleton ruby app. I think its a great idea for us to start using templates such as this one when we have to write one-off scripts.
http://www.infinitered.com/blog/?p=21
Posted by Nate Murray - Feb 9, 2008
Hey guys, here is a fantastic example of wiimote technology. Looks like a low-cost digital whiteboard is nearly here. (Mac version even in development!)
http://www.cs.cmu.edu/~johnny/projects/wii/
I would recommend watching both videos all the way through. The first one is kind of slow but gets better at the end.
Posted by brian - Feb 9, 2008
I have been trying to figure out for a while how to determine the fingerprint of a server. I knew that doing ssh-keygen -l -f id_rsa.pub would yield a fingerprint, but I couldn’t figure out where the fingerprint was kept on a server. I don’t know why this was so hard for me to figure out. No amount of googling seemed to be helping me. But after simply doing a `man ssh` on the server, and then searching for the first appearance of “fingerprint” I found this:
When connecting to a server for the first time, a fingerprint of the server’s public key is presented to the user (unless the option StrictHostKeyChecking has been disabled). Fingerprints can be determined using ssh-keygen(1):
$ ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key
If the fingerprint is already known, it can be matched and verified, and the key can be accepted. If the fingerprint is unknown, an alternative method of verification is available: SSH fingerprints verified by DNS. An additional resource record (RR), SSHFP, is added to a zonefile and the connecting client is able to match the fingerprint with that of the key presented.
*Note* that the file is different on a mac, but doing the same man lookup will tell you where it is. Sorry if most of you already knew this.
Happy ssh’ng
-Brian
Posted by Nate Murray - Feb 9, 2008
I may be only exposing my own ignorance, but I learned something about HTML today that seems so fundamental. I’m very surprised that I didn’t learn this before so I am sharing it here.
Problem: You have lines that are wrapping in weird places. See figure 1. Solution: Add a non-breaking space. See figure 2.
So for example in the solution above you change the code to be “Other Sander Accessories (3)” and the line wraps a little less awkwardly. The non-breaking space is just that, it says “visualize a space but don’t wrap the line here”.
Posted by Nate Murray - Feb 9, 2008
Keep in mind, FizzBuzz is basically an idiot detector. Anybody who bothers to code FizzBuzz is saying, ooh, an idiot detector. I’d better aim this at my own head and see what happens.
- Giles Bowkett
Posted by Nate Murray - Feb 7, 2008
http://www.jingproject.com/ This app is fantastic for screenshots and screencasts. (Also does annotations). Sample image attached.
Recent Comments