Using Grep within BBedit to change some data in tables.
January 6, 2005 3:00 PM   Subscribe

I'm using Grep within BBedit 6.1.2 lite, in order to change some data in tables from "regular" time in the format: "0530PM" to military time: "1730"

Essentially, I am able to locate the data I want using:

(####)PM

Which, in the above example returns: "0530"

So here's the question: I want to treat that result as a number, and add 1200 to it to convert it to military time.

Is there a way to do this within BBedit using the replacement patterns, or do I have to go figure out how to write a perl/python/awk/applescript to accomplish this?
posted by mecran01 to Computers & Internet (9 answers total)
 
my regexp skill is not so great doing conditional results so here is a terrible terrible lazy way...
you could do 12 separate replaces on your set of files, along the lines of:

replace: (05)(\d\d)PM
with: 17\2

replace: (01)(\d\d)PM
with: 13\2

etc. (my bbedit seems to like \d instead of #, but same idea)

personally I would use perl or applescript+bbedit if it's more than just a one-time thing.
posted by dorian at 3:18 PM on January 6, 2005


Response by poster: Thanks very much. I will study your sample code after running it and hopefully learn something.
posted by mecran01 at 3:34 PM on January 6, 2005


perl -pi.bak -e 's/(\d{4})PM/($1+1200)/e' filename

Makes a backup of the original in filename.bak while it's at it, too.
posted by Zed_Lopez at 3:42 PM on January 6, 2005


BBEdit Lite is not scriptable. You won't be able to use AppleScript, Python, etc.
posted by AlexReynolds at 4:34 PM on January 6, 2005


Response by poster: perl -pi.bak -e 's/(\d{4})PM/($1+1200)/e' filename

I've been trying this slick single-line solution, but have hit some probs.

Something in the replace string appears to be el kabong. I can get it working if I remove the .bak extension, and try other replacements:

perl -pi -e 's/(\d{4})PM/cheese/e' filename

works, for example. I have to run it twice, because the original data is in the form of

,0530PM-0730PM,

Anyway, you've given me the basic syntax so now I guess it's time to start reading that copy of "Mastering Regular Expressions" I bought a few years ago.

Oh, I'm running OS X.

Thanks all.
posted by mecran01 at 4:34 PM on January 6, 2005


be careful. 1215 pm is 1215 and not 2415. the solutions above all make this mistake.
posted by andrew cooke at 4:51 PM on January 6, 2005


Oh, I'm running OS X.

On the command-line, you can type "man perlretut" to get the Perl regular expressions tutorial. The "perlre" is pretty useful.
posted by rajbot at 4:56 PM on January 6, 2005


perl -pi.bak -e 's/(\d{4})PM/($1<1200 ? $1+1200 : $1)/ge' filename

This fixes the 2415 bug and the problem by which it wasn't getting multiple hits on one line.
posted by Zed_Lopez at 5:08 PM on January 6, 2005


Response by poster: Last night I ran Neustile's script, which worked great, then this morning I ran your revision, which also worked. I saw the 2415 bug, but there were only two expressions of it in the final, which were easier to fix.

This code is going to become part of a larger project that will take 300 excel entries, plop them into mysql, and make them searchable in complex ways via a web interface. Not that it matters, but in the long run it is going to make things easier for a handful of people in an English department who do scheduling.

Even cooler is I can pick apart your examples and learn to do something incredibly useful with my ibook: command line search and replaces. Again, many thanks for the mini-tutorials.
posted by mecran01 at 8:08 AM on January 7, 2005


« Older Find This Story   |   Photography Freelancing Newer »
This thread is closed to new comments.