Hi all,
I have written a bundle (a macro actually) that edits a file by finding/replacing characters. However I want to add the functionality for it to also delete the last character of each line of the document. I tried using the command moveToEndOfParagraph and DeleteBackward, but it only deletes the last character of the last line of the document. What command(s) would I use for the macro to delete the last character of every line? The number of lines in the document will vary, so I cannot put a hard line count into the macro.
Thank you for your help.
Keith
On Mon, Aug 15, 2011 at 22:41, kravnh kjed@comcast.net wrote:
I have written a bundle (a macro actually) that edits a file by finding/replacing characters. However I want to add the functionality for it to also delete the last character of each line of the document. I tried using the command moveToEndOfParagraph and DeleteBackward, but it only deletes the last character of the last line of the document. What command(s) would I use for the macro to delete the last character of every line? The number of lines in the document will vary, so I cannot put a hard line count into the macro.
Create a macro that regexp-replaces ".\n" with "\n". Or just use a command instead of a macro, say with `sed 's/.$//'`.
HTH, Martin
Hi Martin,
Thanks for your reply. I can do what I want with the command you supplied. However, I don't know how to insert the regexp into my current macro. I am not a developer. I use TextMate for one specific purpose: to change the format of audio timecodes to a syntax that is compatible with an online learning system. It takes a list of Start and End times that look like this: 0'24.9951 Start 0'31.9160 End 0'37.7354 Start 0'47.3503 End
and does the following: adds a leading digit to the first pair, replaces the "'" with a ":", and replaces the "." with a ":", and deletes the TAB-Start and TAB-End. I also need it to delete the last character of each line, so I end with a list like this: 00:24:995 00:31:916 00:37:735 00:47:350
I was able to cobble together a macro that does everything but remove the last digit. I use a series of findWithOptions commands. I have attached the macro I use. What would I need to add to this macro to delete the last character of each line?
Thank you so much for your help. Keith
http://old.nabble.com/file/p32273825/Replace.tmMacro Replace.tmMacro
Martin Kühl-3 wrote:
On Mon, Aug 15, 2011 at 22:41, kravnh kjed@comcast.net wrote:
I have written a bundle (a macro actually) that edits a file by finding/replacing characters. However I want to add the functionality for it to also delete the last character of each line of the document. I tried using the command moveToEndOfParagraph and DeleteBackward, but it only deletes the last character of the last line of the document. What command(s) would I use for the macro to delete the last character of every line? The number of lines in the document will vary, so I cannot put a hard line count into the macro.
Create a macro that regexp-replaces ".\n" with "\n". Or just use a command instead of a macro, say with `sed 's/.$//'`.
HTH, Martin
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
Ah. You can do it almost all of it with a single regular expression search-and-replace. This gets you everything except cleanup on the leading digit. I assume the leading number can go above 9; that makes the regex a little harder if you want to do cleanup. If that's not the case, add a 0 to the beginning of the replace string and it'll do everything you want.
Find: ^(\d+)'(\d\d).(\d+)\d\s+(Start|End)$ Replace: $1:$2:$3
Just record a new macro, do "Replace All" with the above parameters, and save. The search and replace strings will get saved in the macro.
Of course, search and replace isn't the right tool for the job. :-) Here's a little Python script that you can save as a bundle command. Set the input to "Selected Text or Document" and the output to "Replace Selected Text". This script has the benefit of rounding values (0'12.9999 becomes 00:13:000) and dealing with pathological cases (0'99.1234 becomes 01:39.123).
#! /usr/bin/env python import re import sys regex = re.compile(r"^(\d+)'(.*?)\s+(Start|End)$") for line in sys.stdin: mo = regex.match(line.strip()) if mo: ms = round((int(mo.group(1))*60.0 + float(mo.group(2))) * 1000.0) mm = int(ms/60.0/1000.0) ms -= mm*60.0*1000.0 ss = int(ms/1000.0) ms -= ss*1000.0 print '%02d:%02d:%03d'%(mm,ss,ms)
Hi Steve,
Wow, thanks for that script. It does exactly what I need it to do. I truly appreciate your help!
Keith
Steve King-8 wrote:
Of course, search and replace isn't the right tool for the job. :-) Here's a little Python script that you can save as a bundle command. Set the input to "Selected Text or Document" and the output to "Replace Selected Text". This script has the benefit of rounding values (0'12.9999 becomes 00:13:000) and dealing with pathological cases (0'99.1234 becomes 01:39.123).
#! /usr/bin/env python import re import sys regex = re.compile(r"^(\d+)'(.*?)\s+(Start|End)$") for line in sys.stdin: mo = regex.match(line.strip()) if mo: ms = round((int(mo.group(1))*60.0 + float(mo.group(2))) * 1000.0) mm = int(ms/60.0/1000.0) ms -= mm*60.0*1000.0 ss = int(ms/1000.0) ms -= ss*1000.0 print '%02d:%02d:%03d'%(mm,ss,ms)
-- Steve King