GREP a list of files for multiple strings on multiple lines

Grep is most definitely not the most exciting tool to talk about. One of the challenges that I recently faced was to list a bunch of text files only with a specific phrase.

For the sake of context, I was looking through emails of course bookings. Luckily, I know that the courses are prefixed with the word COURSE: and the names. After saving all of the files in a directory it was a case of running this command:

grep -l "COURSE: Course 1" *

Nice and simple. I could then find out which ones were booking Course 2:

grep -l "COURSE: Course 2" *

So far so good. The problem I now had was that I wanted to know exactly how many had booked BOTH courses. The courses aren’t on the same line so it’s not so straight forward but there’s a very simple way to find those pesky files.

grep -l "COURSE: Course 1" * | xargs grep -l "COURSE: Course 2"

By piping the file names from the first result into the argument of the next grep – it was easy to find the files that had both courses booked. You can chain this with the pipe as many times as you need to.

Finally, if you’re feeling really swish, then you might want to know how many matches there are rather than list the files:

grep -l "COURSE: Course 1" * | xargs grep -l "COURSE: Course 2" | wc -l

If you’re looking for some tricks to using grep to match (or not match!) strings, then you should check out this article over at The Geek Stuff.

Get Ripple to work in Linux

As RIM plough forward and deliver a cascade of development tools – one of the key tools is the Ripple Emulator. The Ripple Emulator allows you to simulate a mobile device in Chrome or Chromium and debug it easily. It’s a great app for creating HTML5 apps.

Unfortunately for us Linux users, we have had the short straw as a quick peek over at the BlackBerry site reveals that there is only a download for Windows or Mac.

Not to be put off – I figured that the code must be pretty much the same for both systems and decided that I’d get it working in Linux. Here’s my howto for getting Ripple working with Ubuntu 12.04 Continue reading Get Ripple to work in Linux

Start and connect to your VirtualBox VM with a simple script

The way I like to develop is to create specific virtual machines for my web development.

The chore is to start the machine, mount the file system and then open up your IDE. All a nuisance that takes time. So, I’ve taken to writing a simple Bash script that will do the grunt work for you. All you have to do is put in your settings, save it and run it!

This will mount the VM’s web folder onto your file system and let you know it’s connected. All you need to do is then start your IDE of choice! Continue reading Start and connect to your VirtualBox VM with a simple script

A Quick Way to Manage Multiple Amazon AWS Accounts

I’m often switching between different Amazon AWS accounts. So to speed things along I’ve set up some simple scripts to easily switch between accounts while at the command line

On Linux (aws_setup):

<code>#!/bin/bash
export EC2_URL=https://eu-west-1.ec2.amazonaws.com
export EC2_CERT=/home/john/cert-12345678901234567890123456789012.pem
export EC2_PRIVATE_KEY=/home/john/pk-12345678901234567890123456789012.pem</code>

Once saved, run chmod +x aws_setup
On Windows (aws_setup.cmd):

<code>SET EC2_URL=https://eu-west-1.ec2.amazonaws.com
SET EC2_CERT=C:\Users\John\cert-12345678901234567890123456789012.pem
SET EC2_PRIVATE_KEY=C:\Users\John\pk-12345678901234567890123456789012.pem</code>

Save as many files with the paths to your various AWS key pairs, and simply run the script from within a terminal before using the ec2 tools.

NOTE, when you run the scripts on Linux (and I guess Macs), add an extra dot before you run it. This will allow the environment variable to persist when the script ends.

<code>. ./aws_setup</code>

rsync and the Hard Link Limitation

I’ve been setting up a new backup regime on a hosted server that I’m helping to maintain.

One of the key issues right now is that it is there isn’t a solid provided backup regime, so I’ve spent the day playing around with different Linux backup solutions trying to get to the bottom of a simple, yet robust way of backing up.

I like rsync, but the problem is that you can’t use it’s hard linking feature over secure shell. As such, incremental backups are a pain.

I was about to give up on it, and work out a convoluted tar process when it occurred to me that the hard links might still be possible.

And to that end, I wrote this script: Continue reading rsync and the Hard Link Limitation