Iterate Over Files in Bash

Sometimes you need to whip up a dirty bash script to process through some text. I needed to iterate over my database log files that I wrote about from my last post and then output the result to a new file. I wanted the output files to have a different extension, but I wasn’t sure how to get just the filename from bash, fortunately this post helped.

#!/bin/bash

for logfile in *.log
do

fn=${logfile%.*} #cut extension

echo making $fn.data...
egrep '\#\ collect|\:\=' $logfile > $fn.data 

done

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Follow me on Twitter @jprichardson and read my blog on software entrepreneurship.

-JP

Grep for Two or More Expressions

Unfortunately a database of mine crashed. Fortunately, I have log files with the data. I need the timestamp and the data.

The lines in the log files look something like this:

####### collect $TIMESTAMP #########
*GARBAGE
*GARBAGE
*GARBAGE
$ID:=$DATASET
*GARBAGE

It’s been years since I’ve done anything more than a simple grep. I wasn’t even sure which grep I needed… egrep? fgrep? grep?

I can use this regex for the timestamp line:

\#\ collect

I can use this regex for the dataset line:

\:\=

But how can I put them together? It’s probably of no surpise that I can use the ‘|’ operator like so:

egrep '\#\ collect|\:\=' 2010-09-25.log

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

That’s it! Follow me on Twitter @jprichardson and read my blog on entrepreneurship.

-JP

Get All ProgID on System for COM Automation

If you want to use Silverlight COM Automation, you need to know the ProgID of your COM component. These are buried in the registry. Here is a snippet that I found (don’t remember where) and modified a bit to do this:

var regClis = Registry.ClassesRoot.OpenSubKey("CLSID");
var progs = new List<string>();

foreach (var clsid in regClis.GetSubKeyNames()) {
	var regClsidKey = regClis.OpenSubKey(clsid);
	var ProgID = regClsidKey.OpenSubKey("ProgID");
	var regPath = regClsidKey.OpenSubKey("InprocServer32");

	if (regPath == null)
		regPath = regClsidKey.OpenSubKey("LocalServer32");

	if (regPath != null && ProgID != null) {
		var pid = ProgID.GetValue("");
		var filePath = regPath.GetValue("");
		progs.Add(pid + " -> " + filePath);
		regPath.Close();
	}

	regClsidKey.Close();
}

regClis.Close();

progs.Sort();

var sw = new StreamWriter(@"c:\ProgIDs.txt");
foreach (var line in progs)
	sw.WriteLine(line);

sw.Close();

Are you a Git user? Let me help you make project management with Git simple. Checkout Gitpilot.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur

-JP

Follow

Get every new post delivered to your Inbox.