Author Topic: I wish the Find and Replace dialog could run a sed script  (Read 1000 times)

eitheta

  • Community Member
  • Posts: 17
  • Hero Points: 0
I wish the Find and Replace dialog could run a sed script
« on: February 11, 2015, 10:40:18 pm »
I wish the Find & Replace dialog could run a sed script, rather than just one Search/Replace expression-pair at a time.

Graeme

  • Senior Community Member
  • Posts: 2359
  • Hero Points: 310
Re: I wish the Find and Replace dialog could run a sed script
« Reply #1 on: February 11, 2015, 11:33:01 pm »
I wish the Find & Replace dialog could run a sed script, rather than just one Search/Replace expression-pair at a time.
It might be possible with a macro.  Can you post a simple example of what the sed script would look like and what it's trying to do.

eitheta

  • Community Member
  • Posts: 17
  • Hero Points: 0
Re: I wish the Find and Replace dialog could run a sed script
« Reply #2 on: February 12, 2015, 12:43:54 am »
In general, I'm just massaging a portion of a text file.  With three or four regex substitutions in a row (each one being of middling complexity).  sed would almost be overkill, as I'd probably never use anything except the s/// or s///g command. 
An example:   
Code: [Select]
s/^[ \t]*\/\/.*\n//
s/C_DATA_WIDTH[ \t]*-[ \t]*1/63/
s/KEEP_WIDTH[ \t]*-[ \t]*1/7/
s/^[ \t]+(input|output|inout)[ \t]*\[([0-9]+)[ \t]*:[ \t]*([0-9]+)\][ \t]+([a-z][a-zA-Z0-9_]+),[ \t]*$/  \1    [\2:\3]    ~\4       ,
s/^[ \t]+(input|output|inout)[ \t]+([a-z][a-zA-Z0-9_]+),[ \t]*$/  \1             ~\2       ,
If it gets more complicated than this, then I write a throwaway python script to do the massaging.  If I can do what I need with just one substitution, then of course I do that within SE's Replace dialog. It's when it cannot be done in one step, then I have this discontinuity in my flow.  Instead of the Replace dialog somehow growing smoothly to handle the slightly-more-complex task, I have to jump to a different tool (python, sed, etc).  Back in the day, CodeWright had an embedded Perl interpreter, that could easily serve as a regex search-and-replace on steroids. 
So yes, I think I could create a macro that would run a fileful of s/findpatt/subpatt/g lines.  Perhaps I should give it a go - it might turn out less clunky / more usable than I expect.
Thanks.

rgloden

  • Senior Community Member
  • Posts: 165
  • Hero Points: 5
Re: I wish the Find and Replace dialog could run a sed script
« Reply #3 on: February 12, 2015, 05:26:55 am »
I also would love such a macro that could apply multiple search and replace expressions.  I have a set of Perl regexp's I use to clean up code and its a pain to use 1 at a time.

Graeme

  • Senior Community Member
  • Posts: 2359
  • Hero Points: 310
Re: I wish the Find and Replace dialog could run a sed script
« Reply #4 on: February 12, 2015, 11:47:38 am »
I also would love such a macro that could apply multiple search and replace expressions.  I have a set of Perl regexp's I use to clean up code and its a pain to use 1 at a time.

Using macro recording you can soon see what to do.
In the call to _mfreplace2 below, the third argument has
* = replace all without prompting
I = case insensitive
L = perl reg exp.
The fifth argument ('') is an exclude file list
The sixth argument '4' selects "append to output"
The 7th argument ('1') selects search results tab 1
The final argument selects "don't show diff"


Code: [Select]
_command void run_perl_search_replace1() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   clear_highlights();
   _mfreplace2('abc.def','123.456','LI*','<Workspace>','*.*','','','1','0');
   _mfreplace2('123.456','abc.def','LI*','<Workspace>','*.*','','4','1','0');
   _mfreplace2('etc','etc','LI*','<Workspace>','*.*','','4','1','0');
}
« Last Edit: February 12, 2015, 11:54:59 am by Graeme »

Tim Kemp

  • Senior Community Member
  • Posts: 536
  • Hero Points: 90
Re: I wish the Find and Replace dialog could run a sed script
« Reply #5 on: February 12, 2015, 11:59:38 am »
@rgloden and @eitheta, Edit->Other->Filter Selection... allows you to pipe a selected portion of a file through any external program. If you have a filter that is more complicated than you can easily describe in a regex, this is an easy way to bring more sophisticated tools to bear.