Author Topic: global replace macro  (Read 6637 times)

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
global replace macro
« on: July 18, 2007, 09:35:23 pm »
Just learning to use the Slick macros and am having trouble figuring out how to create one that will perform a global replace in the current file (buffer?)

All I want is to create one that removes the many timestamps from a log file so I currently do this manually:

Ctrl-R (open the replace window)
Enter ??:??:??.?? ? <=no spaces...didn't want the smiley to show up :)
Enter 00:00:00.000
Ctrl-A (i.e. [Replace All])
Ctrl-S save changes.

In the replace window, I have it set to use Wildcards (*,?) and to wrap at beginning/end.

I wanted to create a simple macro attached to a key combination to handle it.  This doesn't seem to do it or at least the documentation doesn't really tell me how to do a "replace all":

   replace("??:??:??.?? ?", "00:00:00.000", "I");

OK, the "I" is probably not needed, but I couldn't find any other options to handle the global replace.

I'd appreciate a working macro for this or another solution so I don't have to use a different tool to handle this step.

(Oh, using Slick 11.0.2)

Lisa

  • Senior Community Member
  • Posts: 238
  • Hero Points: 23
  • User-friendly geek-speak translator extraordinaire
Re: global replace macro
« Reply #1 on: July 18, 2007, 09:54:44 pm »
I'm not a pro, but in 11.0.2, I used these steps to create a similar macro:

1. Click Macro > Record Macro
2. Click Search > Replace
3. Enter your parameters
4. Click Replace All
5. Click Macro > Stop Recording Macro
6. Enter the Macro Name
7. Click Add Key or Mouse Click
8. Press the keybinding you want to use
9. Click Bind
10. Click Done

Now I can press the keybinding I assigned in any file to perform the Replace All operation. Does this help or are you looking for something more advanced?

Lisa

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #2 on: July 18, 2007, 10:28:27 pm »
Thanks Lisa.

I had previously tried that and got an empty macro.

I you can do that for me with the above values and paste the code here, I'll try it.


jbhurst

  • Senior Community Member
  • Posts: 405
  • Hero Points: 33
Re: global replace macro
« Reply #3 on: July 18, 2007, 11:09:20 pm »
Hi,

Try "*" with or instead of your "I" option. The "*" option means "replace all without prompting". It is documented in the online help for the replace() function. (Should be documented for 11.x; but I'm looking at 12.0.2.)

Regards

John Hurst
Wellington, New Zealand

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #4 on: July 18, 2007, 11:16:16 pm »
Thanks, jb.

I see "*" in the doc but didn't realize it would apply here.

I tried this:
_command tttt() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   _macro('R',1);

   replace("??:??:??.?? ?", "00:00:00.000", "I*");

   //save();
}

but when I run it simply highlights the "next" occurrence then stops.  It doesn't replace it and certainly doesn't replace all.

Any other ideas?

(I'll check in tomorrow.)

...BTW...is there a way to subscribe to this so I get e-mail notifs?
« Last Edit: July 18, 2007, 11:19:19 pm by rspahitz »

hs2

  • Senior Community Member
  • Posts: 2744
  • Hero Points: 288
Re: global replace macro
« Reply #5 on: July 18, 2007, 11:28:55 pm »
Try replace("??\\:??\\:??\\.?? ?", "00:00:00.000", "IRP*");
(R == SlickEdit regex, P == wrap if not found)

Quote
...BTW...is there a way to subscribe to this so I get e-mail notifs?
Click on 'Profile' -> Notifications and Email

Good luck,
HS2
« Last Edit: July 18, 2007, 11:40:15 pm by hs2 »

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #6 on: July 19, 2007, 03:52:35 pm »
hs,

(thanks for the notif info)

Since we're dealing with regex, I figured I'd change the ?s to regex (although I'm no expert so I may have gotten it wrong.)

I used this but it still only finds the time but doesn't replace it:

   replace("[0-9][0-9]\\:[0-9][0-9]\\:[0-9][0-9]\\.[0-9][0-9][0-9]", "00:00:00.000", "IRP*");

Is it really that hard to do this, or are my settings weird?

jbhurst

  • Senior Community Member
  • Posts: 405
  • Hero Points: 33
Re: global replace macro
« Reply #7 on: July 19, 2007, 06:55:25 pm »
Hi,

BTW Have you tried the Regex Evaluator tool window (View > Toolbars  Regex Evaluator)?

This can somtimes help you figure out regular expressions.

Check out the help for some shortcuts too.  In UNIX style regular expressions you can match two digits using \:d{2} rather than [0-9][0-9], in case you think that is an improvement.  In Slick regular expressions I think it's :d:2, which is quite a nice improvement.

John Hurst
Wellington, New Zealand

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: global replace macro
« Reply #8 on: July 19, 2007, 07:01:52 pm »
If your wanting to use the Wildcard syntax, your need to pass the '&' option instead of the 'R' option:

Code: [Select]
replace("??:??:??.???", "00:00:00.000", "IP&*"); //& symbol specifies wildcard symbol in options
If you using SlickEdit syntax, it looks like you're not escaping the special characters correctly, try this instead:

Code: [Select]
replace("[0-9][0-9]\:[0-9][0-9]\:[0-9][0-9]\.[0-9][0-9][0-9]", "00:00:00.000", "IPR*");
You do need escape character '\' in front of any special tokens (like the ':' and '.') in the string so that they are interpreted as literals. A '\\' is interpreted as a literal character '\'.  There are also several handy shortcuts, like :d instead of [0-9] depending on the chosen syntax.   Check the help guide for a complete list of regular expression tokens and syntaxes.



rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #9 on: July 19, 2007, 07:58:09 pm »
thanks for the tips, JB and Lee.

Here's the sample data I'm using for the macro:

--
DU550000
xxx11:22:33.444
vvv11:22:33.445
z11:22:33.446

gsdfgdf
--

And the macro now looks like this:

Code: [Select]
_command tttt() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   _macro('R',1);

   replace("??:??:??.???", "00:00:00.000", "IP&*");
   //replace("[0-9][0-9]\:[0-9][0-9]\:[0-9][0-9]\.[0-9][0-9][0-9]", "00:00:00.000", "IRP*");

   //save();
}

For now, I run the macro through the menu (Macro, List Macros..., select "tttt", then click the Run button)

When it runs, it searchs down and finds the next time string, excluding the microseconds, such as:

11:22:33

-----------------
Any thought on why it doesn't select everything?
(The same happens whether I use the wildcard or regex replace statement!)

Can someone try it and tell me if you get the same results?  If not, there must be a setting difference.

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: global replace macro
« Reply #10 on: July 19, 2007, 08:08:58 pm »
Could you list a specific example thats not working for you?  From what information you've given, I'm not exactly sure what's not working correctly.  A before and after example would be handy.

jbhurst

  • Senior Community Member
  • Posts: 405
  • Hero Points: 33
Re: global replace macro
« Reply #11 on: July 19, 2007, 08:14:43 pm »
Just a thought -- are you making sure you're loading (CUA: F12) the macro when you change it? Just editing the file is not enough...

Regards

John Hurst

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #12 on: July 19, 2007, 08:31:42 pm »
Lee, our messages must have crossed paths.

John, thanks.  I assumed that opening the macros and clicking the save button should be enough.  So I closed the macro window but so far still the same results.  Can I pop up a messagebox to at least le me know that I'm actually in the right place?

jbhurst

  • Senior Community Member
  • Posts: 405
  • Hero Points: 33
Re: global replace macro
« Reply #13 on: July 19, 2007, 08:48:05 pm »
Hi,

Closing the macro window doesn't do anything special. You need to invoke load. (SlickEdit code is compiled, not interpreted.) With the CUA emulation (the default) you can invoke load by pressing F12 when the macro code is in the current buffer.

You can display a message box by adding something like this:
Code: [Select]
_message_box("Hello");

Hope this helps.

John Hurst
Wellington, New Zealand

rspahitz

  • Community Member
  • Posts: 7
  • Hero Points: 0
Re: global replace macro
« Reply #14 on: July 19, 2007, 09:05:16 pm »
cha-ching!

I guess I needed to reload the macros.

All works great now, and I learned a few things.

Thanks everyone!