Tapatalk

Script to add a string at the end of line

Script to add a string at the end of line

8

PostDec 13, 2022#1

I need a script which will search for <CM> and if found on a line in the file it must append <CM> to the end of that line.

Example: The line contains:

Code: Select all

die vyfde met <W •sardoniks><CM>die sesde met <W •karneool><CM>die sewende met <W •chrisoliet><CM>die agtste met <W •berilsteen><CM>die negende met <W •topaas><CM>die tiende met <W •chrisopraas><CM>die elfde met <W •hiasint><CM>en die twaalfde met <W •ametis>.
 It must end up like this:

Code: Select all

die vyfde met <W •sardoniks><CM>die sesde met <W •karneool><CM>die sewende met <W •chrisoliet><CM>die agtste met <W •berilsteen><CM>die negende met <W •topaas><CM>die tiende met <W •chrisopraas><CM>die elfde met <W •hiasint><CM>en die twaalfde met <W •ametis>. <CM>
Is that possible?

I use UltraEdit version 18.10.0.1018.

6,826625
Grand MasterGrand Master
6,826625

PostDec 13, 2022#2

A Perl regular expression replace all with (<CM>.+)$(?<!<CM>) as search expression and \1 <CM> as replace expression appends a space and <CM> at end of every line containing <CM> and not already ending with <CM>.

Recorded as UltraEdit macro:

Code: Select all

InsertMode
ColumnModeOff
HexOff
Top
PerlReOn
Find MatchCase RegExp "(<CM>.+)$(?<!<CM>)"
Replace All "\1 <CM>"
Written as UltraEdit script:

Code: Select all

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

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

   // Define all parameters for a case-sensitive Perl regular expression
   // replace all executed from top of the file and execute the replace.
   UltraEdit.perlReOn();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   // In JavaScript strings a backslash is interpreted as escape character
   // and must be escaped itself with a backslash to define the regular
   // expression replace string with a literal interpreted backslash by
   // the JavaScript core engine.
   UltraEdit.activeDocument.findReplace.replace("(<CM>.+)$(?<!<CM>)", "\\1 <CM>");
}

8

PostDec 13, 2022#3

Thank you very much! It does exactly what I wanted.