Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • October 23, 2017, 06:49 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Text editor - macro - multiple regex find and replaces  (Read 2237 times)

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 304
    • View Profile
    • Donate to Member
Text editor - macro - multiple regex find and replaces
« on: January 23, 2017, 09:37 AM »
Up front, I am totally unfamiliar with the following.

I would like to do this:
have a text in a text-editor
then run a macro (or something like that) that performs multiple find-and-replace actions based on regex
on selected text

so, a kind of
find regex #1
replace with so and so
find regex #2
replace with another so and so
find regex #3
etc.

probably it should be done in a script(?)
I can do a find and replace one by one only but that is a lot of work.

I don't know if there are text editors that can create macro's using multiple regex.

Vainly spent a -lot- of time on finding anything that may lead me into the right direction.

Anyone out there has a tip?

I believe there are no macro recorders that can record multiple find-and-replace actions.

Thanks!

=

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,039
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #1 on: January 23, 2017, 09:54 AM »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,525
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #2 on: January 23, 2017, 10:48 AM »
For repetitive commands you may wish to take a look at vim

It is free and is capable of operations difficult in most free text editors.  It has been years since I have used it.  So I forget the particular commands.  But I can remember doing things like cutting all but the first sentence in each paragraph in a file and pasting it at the bottom of the file.  Something like that in a lengthy file can quickly tire you out if you physically do the cut and paste operations manually.  With vi(on which vim is based) it was done with one command.

I keep saying I will get back into using it myself.  But I procrastinate.  :)

I am sure it supports regular expressions.

Also for regex substitutions on each line of text processing entire files see the Linux "sed" tool.  There are many "one-liners" you can find on the web.  There is a good chance what you want to do is already possible using one of them.  But briefly sed is a "stream editor."  It is used on the command line.  You feed a file(which can also be standard input such as via a pipe on the command line) into it and it processes the text using the command and outputs the result.  You can find sed for Windows as freeware if you are using Windows.

Note that with sed one-liners for Linux the file separator is a forward slash('/') instead of a backslash('\') and for that reason it is a good idea to look for a one-liner page for the Windows version of sed.  That would save you having to edit the one-liner, which somewhat defeats the purpose of finding a "canned" solution to the problem.  :)

You may be able to find a newer version of sed for Windows than this but for command line tools it is possible they have not changed all that much since 2010 anyway.
« Last Edit: January 23, 2017, 10:59 AM by MilesAhead »

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,899
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #3 on: January 23, 2017, 12:37 PM »
Also, Emacs. Although both Vim and Emacs lack PCRE support (except you call Perl from them).  :)

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 2,943
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #4 on: January 23, 2017, 03:01 PM »
You could do it with Notetab Pro, which supports PCRE, though I'm not sure if the latest version.  I don't know if freeware Notetab Light can also do "clipbook programming," their name for Notetab's macro language.

Otherwise, MilesAhead's suggestion of sed is sound.  Another useful source is the seder's grab bag.  If sed can't do what you want, or its syntax is too cryptic, try AWK; Awk  FAQ.  Notetab Pro knows how to run AWK scripts, also Perl scripts.

Another approach: try Andrew Pipkin's Minitrue, a Search / Replace Utility (Named after Orwell's "1984").  It supports extended regular expressions.  If you're on Windows, you might want to try the version of Minitrue recompiled by Jason Hood to better support Windows LFNs.

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,139
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #5 on: January 24, 2017, 02:43 AM »
Already good answers, here are some more....

You could use EmEditor Text Editor
- you can record a search&replace in a macro, edit that macro by copying the command line, and run this.
https://www.emeditor...y/scriptable-macros/
- you can also use the search&replace dialog box and enable Batch-Replace:
https://www.emeditor...tures/batch-replace/

You could use PowerShell (new WindowsTM batch tool)
Command line usage:
(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt
http://stackoverflow...file-with-powershell
Script usage:
$current=(Get-Content c:\temp\test.txt)
$OUT=$current.replace('[MYID]', 'MyValue')
$OUT=$OUT.replace('XXX', 'ABC')
$OUT | Set-Content c:\temp\test.txt

You could use AutoHotkey:
FileRead, varOrigin, test.csv
StringReplace, varOUT, varOrigin, xxx, ABC, All
StringReplace, varOUT, varOUT, YYY, DEF, All
FileDelete, test.csv
FileAppend, %varOUT%, test.csv
https://autohotkey.c...eplace-in-text-file/

You could use MS-Word with VBA
- start record macro
- call search&replace , do your first replacement
- stop recording
- click Macros > Modify
- copy the replacement block of code and modify to your second replacement need
- save and execute
There are more elegent ways with ARRAYs and FOR EACH in Array DO... but for the start that should work for you too.

MS-Word supports Wildcards: http://word.mvps.org...l/UsingWildcards.htm

Within a macro, you can utilize VBS/VBA RegEx (Set regExp = CreateObject("vbscript.regexp"))
http://superuser.com...replace-in-word-2010
http://stackoverflow...alternation-operator

Best way is to copy a own (found) function into the macro code and call that function for the replacement work:
http://bytecomb.com/...-expressions-in-vba/
>>> RxReplace VBA Function
Function RxReplace( SourceString, Pattern , ReplacePattern , IgnoreCase , MultiLine , MatchGlobal )


Just some ideas as starting point....


 

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 304
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #6 on: January 24, 2017, 03:41 AM »
First of all: MANY thanks for the feedback!
It is really appreciated.

after a lot of searching I think UltraEdit can do this.

one starts to record a macro, does 1 find and replace in regex
stop recording
save the macro and then add all extra find-and replaces on top.
probably it isn't vbscript as the macro looks very simple, but ... effective and easy to expand

InsertMode
ColumnModeOff
HexOff
PerlReOn
Find RegExp "^-bla-bla"
Replace All "$1-$2-$3"
Find RegExp "^bla bla"
Replace All "$4-$5-$6"
Find RegExp "bla bla"
Replace All "$7-$8-$9"

etc.

I have looked at all the above, except for the last reply from abterix. Still need to do that.
But believe UE is relatively straightforward, whereas the above suggestion do have a bit of a 'learning curve',  so to say.
UE issn't cheap though, I must think about it.

@AbteriX

Reverting on your feedback.

=



anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 473
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #7 on: January 24, 2017, 03:51 AM »
Hi dcwul62,

I will like to add below which are specific for your purpose.
ultraedit http://www.ultraedit.com/ not free. I use it for the type of job as listed by you.

Further for search and replace with multiple times, below can do the job
powergrep https://www.powergrep.com/ not free
textcrawler https://www.digitalv....uk/textcrawler.html has free version

Regards,

Anand

Note: As I was posting, I found your reply. So you found UltraEdit. Nevertheless I am posting my reply.


AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,139
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #8 on: January 24, 2017, 05:44 AM »
A freeware Text Editor is PSPad, which can be scripted with VisualBasicScript (VBS)

Store that code as MultiReplace.vbs in "...\PSPad\Script\VBscript\" folder.
In PSPad run menu "Scripts\Recompile", next see new entry "Multiple Replace on downloaded text".


Quick&Dirty Example:
'*******************************************************************************
' Filename : X\PSPad\Script\VBscript\MultiReplace.vbs
' Description :
' Created :
' Created by :
' Note   :
' You may distribute this script freely, but please keep this header intact.
'*******************************************************************************
'OPTION EXPLICIT
'ON ERROR RESUME NEXT

Sub Init
        '//addMenuItem MODULE_Title, "sub menu" , MODULE_NAME_to_run ,"ctrl+shift+alt+m"
        addMenuItem "Multiple Replace on downloaded text", "" , "Main"
End Sub
'*******************************************************************************
Sub Main()
        Set editor = newEditor()
        editor.assignActiveEditor()
        strInputSel = editor.selText()


        strOut = strInputSel     
        '//==================================================== === START User Settings ===
         '//   strOut = runRegExpReplace(strOut, "yourSearchPattern" , "yourReplacement")
         strOut = runRegExpReplace(strOut, "X", "Y")
         strOut = runRegExpReplace(strOut, "-", "....")
         strOut = runRegExpReplace(strOut, "this", "that")
         strOut = runRegExpReplace(strOut, " the the ", " the ")
         '.... and so on
        '//==================================================== === END of User Settings ===

         '//write back:
        editor.selText strOut
        Set editor = Nothing
End Sub


Function runRegExpReplace(haystack, Pattern, newtext)
    Set regEx = New RegExp
    If Pattern = "" Then
        runRegExpReplace = haystack
        Exit Function
    End If
    regEx.Pattern        = Pattern
    regEx.IgnoreCase = TRUE
    regEx.Global          = TRUE
    regEx.Multiline      = TRUE
    runRegExpReplace = regEx.Replace(haystack, newtext)
End Function





.
« Last Edit: January 26, 2017, 01:33 AM by AbteriX, Reason: Added instructions how to use. »

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 304
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #9 on: January 25, 2017, 07:59 AM »
Abterix - thanks again! Appreciate this.
I checked out PSPad and did the following simple things.
existing text
start recording macro
do a find and replace
stop macro
save macro
(same to .pme ?)
then back to macro - edit macro - empty

tried a few times, but gave up.

Also tried the famous Notepad++ but that editor also doesn't seem to support editing macros.
At least not as simple as UltraEdit.
someone else also asked that
https://notepad-plus...o-i-m-new-to-notepad
but as a new user one is referred to a rather complicated page that requires a lot of reading and stuff, so I dropped Notepad++

Then there are a lot of other editors like BBEdit, Sublime Text, Atom, etc. but those are focused on writing code, which is not for me.

There is still Notetab (Need to check it out)
EditPadPro is hardly ever mentioned.

=

dcwul62

  • Supporting Member
  • Joined in 2013
  • **
  • default avatar
  • Posts: 304
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #10 on: January 25, 2017, 08:10 AM »
Notetab - no macro as far as I can see.
After installation, looking around for macro features, then closing and relaunching, the trial period is over
and one is sent to the Purchase website. ha!

=

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,899
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #11 on: January 25, 2017, 08:26 AM »
Then there are a lot of other editors like BBEdit, Sublime Text, Atom, etc. but those are focused on writing code, which is not for me.

Sublime Text is not focused on writing code.

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,252
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #12 on: January 25, 2017, 09:49 AM »
Another piece of software you could try: RJ TextEd

It is offered as freeware and looks to have a simple enough macro recorder. Looks like this editor has a lot of extra options, besides text-editing. I have used to search-replace content in really big text files and it does this very, very well.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 2,943
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #13 on: January 25, 2017, 02:44 PM »
Notetab - no macro as far as I can see.
I should have been clearer.  It doesn't have "watch my fingers, record and repeat" macros.  Instead it has a macro programming language, what they call a clipbook.  That is, you have to write code.  In the attached screenshot, the tabs along the bottom of the editing window are some of the different clipbooks Notetab Light ships with.  The narrow pane at the left is the contents of the selected clipbook.  Toggle that pane on/off with F4.

After installation, looking around for macro features, then closing and relaunching, the trial period is over and one is sent to the Purchase website. ha!
I wouldn't have expected that.  Still, save yourself some money and try out the Light version.  You can download it here.  I used it for the screenshot to show it has the clipbook system, though I'm told its engine works a bit differently than Notetab Pro.  Apart from the Help file, the best place to get help on the Notetab editors is the three Yahoo! Groups for beginners, clips, and scripts, of which clips is by far the most popular.

NTL_clipbook.pngText editor - macro - multiple regex find and replaces

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 8,945
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #14 on: January 25, 2017, 02:59 PM »
Then there are a lot of other editors like BBEdit, Sublime Text, Atom, etc. but those are focused on writing code, which is not for me.

Sublime Text is not focused on writing code.

Agreed.  Many people use it for that (including me), but it's not focused on that.  In fact, I also use it for writing.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 2,943
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #15 on: January 25, 2017, 03:34 PM »
Another piece of software you could try: RJ TextEd
It sort of grew over the last seven years:
RJ_TextEd_NoInstall.zip    9446479   2009.03.05
rj-texted.7z              99580128   2016.12.26


Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,252
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #16 on: January 25, 2017, 06:17 PM »
11 times the size ?!?

Impressive!

Think I found out about this editor 2 years ago. And although its interface looks a bit busy to me, it is a capable text editor.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,899
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #17 on: January 25, 2017, 06:31 PM »
RJ TextEd is more than twice the size of Emacs now?

Well...

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 8,945
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #18 on: January 25, 2017, 07:20 PM »
Another piece of software you could try: RJ TextEd
It sort of grew over the last seven years:
RJ_TextEd_NoInstall.zip    9446479   2009.03.05
rj-texted.7z              99580128   2016.12.26



Could it be the addition of unicode that ballooned it?

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,139
    • View Profile
    • Donate to Member
Re: Text editor - macro - multiple regex find and replaces
« Reply #19 on: January 26, 2017, 01:57 AM »
I checked out PSPad and did the following simple things.
existing text
start recording macro
do a find and replace
stop macro
save macro
(same to .pme ?)
then back to macro - edit macro - empty

Just to be clear, PSPad was not meant as example of an good macro recorder  :)
in fact, PSPad macro didn't record such Find&Replace dialogs.

My PSPad post was to provide you a VBScript to perform your task,... I have now added an how-to use above:
Quote
Store that code as MultiReplace.vbs in "...\PSPad\Script\VBscript\" folder.
In PSPad run menu "Scripts\Recompile", next see new entry "Multiple Replace on downloaded text".


PSPad is free an have Scripting support, but even better for my use and with
an far better macro recorder is EmEditor, which costs $40 (or $150 with more than 1 year update service)


.