Advertise here: Contact FM.


January 6, 2005
3:00 PM   RSS feed for this thread 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 (10 comments 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


not easily, I made a one minute (very bad code but works) perl script though. save it to "whatever.pl" and run it in terminal like "perl whatever.pl < inputfile> outputfile"

while(<>) {
while(/([0-9]{4})PM/) {
$mil = int($1)+1200;
$str = "$1". "PM";
s/$str/$mil/;
}
print;
}
posted by neustile at 3:20 PM on January 6, 2005


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


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


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 What with the Archbishop of Ca...   |   I've been interested in photog... Newer »

You are not logged in, either login or create an account to post comments



Related Questions
Coloring an OSX Terminal window based on an ssh... May 15, 2008
Help me batch process files and folders with... October 17, 2007
'Date Created' -> 'Date Modified' August 6, 2007
Stripping some (but not all) formatting from rtf... May 2, 2007
Is there a PC equivalent to AppleScript? September 25, 2006