Search and replace with term list?

妖精的绣舞 提交于 2019-12-13 04:56:09

问题


I wonder if there is a program that I can use with a list of terms I want to replace instead of take one by one.

Example

À=À
â=â
Â=Â
å=å
Å=Å
ã=ã
Ã=Ã

Thank you in advance

I use UltraEdit and powergrep atm.


回答1:


UltraEdit has 2 features for automating reformatting tasks: macros and scripts.

See UltraEdit forum topic When to use Scripts over Macros for a brief overview of the differences.

UltraEdit macro

An UltraEdit macro can be created by simply recording the replaces you manually do once on a file or you code them directly in Edit/Create Macro dialog.

A manual creation is done as follows:

  1. Click in UltraEdit in menu Macro on menu item Edit Macro.
  2. Click on button New macro.
  3. Enter as macro name for example ReplaceEntities.
  4. Uncheck macro property Show cancel dialog for this macro.
  5. Let macro property Continue if search string not found checked.
  6. Assign a hotkey or chord for quick execution by key if this is wanted for quick macro execution by key in future.
  7. Click on button OK.
  8. Back in Edit/Create Macro, there is now the new macro selected with 3 lines already present in edit field with the macro commands:
    InsertMode
    ColumnModeOff
    HexOff
  9. Below those 3 macro commands must be added for this reformatting task the macro code lines posted below.
  10. Click on button Close and confirm the question to update the macro with button Yes.

The macro is then ready for usage

  • by assigned hotkey/chord,
  • by double clicking on it in Macro List opened via View - Views/Lists - Macro List in case of not being already visible (docked or floating), or
  • by using Macro - Play Any/Multiple Times.

Also Macro - Play Again can be very often used depending on which macro was last time executed.

The macro code required additionally to 3 standard commands already present in the dialog to replace in entire active file HTML entities is for example:

Top
UltraEditReOn
Find MatchCase "À"
Replace All "À"
Find MatchCase "â"
Replace All "â"
Find MatchCase "Â"
Replace All "Â"
Find MatchCase "å"
Replace All "å"
Find MatchCase "Å"
Replace All "Å"
Find MatchCase "ã"
Replace All "ã"
Find MatchCase "Ã"
Replace All "Ã"

It is necessary to save this UE macro (without or with other UE macros) into a macro file by using Macro - Save All.

For using this macro (and other macros store in same macro file) later again, it is necessary to load the macro file using Macro - Load.

With Macro - Set Auto Load it is possible to select a macro file for being automatically loaded on startup of UltraEdit so that the macros in this macro file are available from beginning without an explicit loading of the macro file.

The macro properties can be also changed later by using Macro - Delete Macro/Modify Properties. Don't forget to use Macro - Save All after making a change to a macro code or its properties to save this change in macro file, too.

UltraEdit script

UltraEdit scripts make use of the JavaScript core engine. An UltraEdit script is an ASCII/ANSI text file containing JavaScript core code with additional UltraEdit related scripting commands. This means an UltraEdit script can be written directly like any other text file and must not be edited in a dialog.

An UltraEdit script which makes exactly the same as the macro above would be:

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.hexOff();

   // Move caret to top of the active file.
   UltraEdit.activeDocument.top();

   // Define all parameters for several Replace All in entire active file.
   UltraEdit.ueReOn();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=false;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
   UltraEdit.activeDocument.findReplace.selectText=false;
   // This property is only available since UE v14.10.
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }

   UltraEdit.activeDocument.findReplace.replace("À","À");
   UltraEdit.activeDocument.findReplace.replace("â","â");
   UltraEdit.activeDocument.findReplace.replace("Â","Â");
   UltraEdit.activeDocument.findReplace.replace("å","å");
   UltraEdit.activeDocument.findReplace.replace("Å","Å");
   UltraEdit.activeDocument.findReplace.replace("ã","ã");
   UltraEdit.activeDocument.findReplace.replace("Ã","Ã");
}

Such an UltraEdit script should be saved with file extension .js, for example ReplaceEntities.js.

Once an UE script is saved, it can added via Scripting - Scripts to the Script List with adding a short description for the script and assigning a hotkey/chord to the script for quick execution by key.

The script is then ready for usage

  • by assigned hotkey/chord,
  • by double clicking on it in Script List opened via View - Views/Lists - Script List or Scripting - Script List in case of not being already visible (docked or floating), or
  • by clicking on script file name in menu Scripting.

If an UE script is the active file AND it is written to run NOT on active document, the script can be also executed with Scripting - Run Active Script. But most scripts like the one above are written to run on active file and therefore requires adding the script file to the script list for execution.

The core objects and functions of JavaScript are not documented anywhere within UltraEdit although they can be also used in UltraEdit scripts. The documentation for the core features can be found on Mozilla Developer site.

The additional scripting commands of UltraEdit are documented in help of UE on page with title Scripting commands. There is additionally View - Views/Lists - Tag List containing the tag groups UE/UES Script Commands and also UE/UES Macro Commands for quick adding a scripting or macro command of UE in active file at current position of the caret.




回答2:


Here is my umlaut2html-Makro which does a few automated text-replacements. I guess it can serve as inspiration ;-)

// Lessons learned from Mofi ;-)
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.selectText=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.replace("ä","ä");    
UltraEdit.activeDocument.findReplace.replace("ö","ö");    
UltraEdit.activeDocument.findReplace.replace("ü","ü");    
UltraEdit.activeDocument.findReplace.replace("Ä","Ä");    
UltraEdit.activeDocument.findReplace.replace("Ö","Ö");    
UltraEdit.activeDocument.findReplace.replace("Ü","Ü");    
UltraEdit.activeDocument.findReplace.replace("ß","ß");    

Save this into a .js-file anywhere you like (MyDocuments\UE-Scripts might be a good choice), then call Script > "Scripts..." and "Add", navigate to select that .js-file.



来源:https://stackoverflow.com/questions/30641085/search-and-replace-with-term-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!