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.

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>

Move a print job from one queue to another in Linux

Today, I was looking at my printer queue and realised that Claire had tried to print something from the internet to my work printer. This meant that unless I went to the office – it would not print. A problem.

Because Claire had decided to use my laptop rather than one of the desktops, I was set up for Linux and believed that I could solve this problem someone in a very tekky way.

A quick squint at the internet found Linux Commando’s How to move print jobs from one printer queue to another.

Perfect!

So, to break it down – here’s what you do. Let’s say that you’ve printed to printer B, but you meant to print to printer A, this is what we do.

Step 1

Find the print job number in the queue. The easiest way is at the command prompt

<code>lpq -P printerB</code>

This will show a list of jobs:

printerB is ready and printing
Rank    Owner   Job     File(s)                         Total Size
active  john    180     12 Steps to a Success with Your 182272 bytes

Now we can see that your print job is 180. Great.

Step 2

So I want to move that job to printerA:

<code>sudo lpmove printerB-180 printerA</code>

That’s it. Now if you check the queue for printer A:

<code>lpq -P printerA</code>

You will see the print job printing out. Hurrah!

Deleting files older than a specific date or age

I needed to delete a chunk of cookies today – but I didn’t want to delete them all.

Thankfully there is a handy command that is within Windows versions after XP such as Server 2003 and Vista that will enable to clean up without too many problems.

The forfiles command will allow you to set a specific date for a file so that you don’t delete recent cookies:

<code>forfiles /S /p "C:\Users\John\cookies" /D -150 /c "cmd /c del @path"</code>

If you wanted to be super cool and delete multiple user cookie folders you could do something like this:

<code>for /D %D in (C:\Users\*) DO forfiles /S /p "%D\cookies" /D -150 /c "cmd /c del @path"</code>

This will enumerate all of the user folders in the C:\Users folder and then clean up the cookies folders inside.

If you wanted to – you can change the amount of days from 150 days to any other amount you like by changing /D -150 to any other number of days (or even use a specific date).