UltraEdit Script

Help with writing and running scripts

UltraEdit Script

Postby mane1034 » Wed Mar 06, 2013 7:04 am

Hi,

I am new to this forum and scripting world!!!!

Luckily I hav UltraEdit installed on my PC so I manage to write few scripts and they worked.

I am struggling to write a simple script to replace different characters in a file.

I created following script which works fine as long as the selected txt is manually entered. If I copy contents from another file then it does not work:

UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.selectText=true;
UltraEdit.activeDocument.findReplace.replaceAll=false;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.replace("a","x");
UltraEdit.activeDocument.findReplace.replace("b","y");

abABab
BAabAB

Any help will be appreciated.
mane1034
Newbie
 
Posts: 5
Joined: Wed Mar 06, 2013 6:41 am

Re: UltraEdit Script

Postby mane1034 » Wed Mar 06, 2013 7:50 am

I got this working with below script:

UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replace("a", "d");
UltraEdit.activeDocument.findReplace.replace("b", "e");

Now the above replces the mentioned characters everywhere in the file.

Now I got another requirement. Could I add exceptions in the above script.

For example in below file

a
b
ab
add
bad

I wanted to excude any replacement with words 'add' and 'bad'. The script should not change these words in the file?

I am in love with the scripting !!

Thanks again.
mane1034
Newbie
 
Posts: 5
Joined: Wed Mar 06, 2013 6:41 am

Re: UltraEdit Script

Postby Mofi » Thu Mar 07, 2013 9:34 am

First, if you run finds/replaces from within a script, always define at least once for every document you run the finds/replaces on ALL parameters. Do not create scripts which depend on interal defaults of UltraEdit as they can change in future. Here is a block to define the search engine and all replace parameters.

Code: Select all
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
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=false;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

Not included are selectText (just seen it the first time) as if a find/replace should be done within a selection only or anywhere is usually controlled by the mode parameter. And also not included are parameters like fromCol which are evaluated only if an associated parameter in the list above is set different like the searchInColumn parameter.

Your first incomplete script was obviously written for replacing just first character a by x in selected text only. As a single replace results in discarding the selection, the second single replace just replaced the next character b by y.

You second incomplete script replaces now from top of file all a by d and all b by e.

What you want know is completely unclear for me. So I cannot really help you. I'm quite sure that you do not really replace single characters as this does not make much sense. I suppose you want to replace words. There is the parameter matchWord which could be used to avoid replacing words which are just substrings in larger words.

If you want a detailed help, we need a detailed example what should be done.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: UltraEdit Script

Postby mane1034 » Thu Mar 07, 2013 11:10 am

Thansk for your reply:

Here is detailed description:

Actually the requirement is to change the real fields contents in a delimeited files so they can be used in test environment.

For example see below input contents:

blah;blah;add
one;blah;odd;12;four

based on above I wanted change the contents of the files so it replaces the characters all around in every field except for the set of fields as they are needed to be there. In above example assume words one, three and four should not be replaced or change.

so I need output like:

xyzk;xyzk;zpp
one;three;ipp;12;four

Thanks again.
mane1034
Newbie
 
Posts: 5
Joined: Wed Mar 06, 2013 6:41 am

Re: UltraEdit Script

Postby Mofi » Fri Mar 08, 2013 1:20 am

For your weird example the following script produces the output you want:

Code: Select all
UltraEdit.activeDocument.top();
UltraEdit.perlReOn();  // Using Perl regular expression engine.
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;       // Regular expression ON
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;    // Replace All ON
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

// Replace word "blah" anywhere within first value of a row by "xyzk".
UltraEdit.activeDocument.findReplace.replace("^([^;\\r\\n]*)blah", "\\1xyzk");
// Replace word "blah" anywhere within second value of a row by
// "xyzk", but only if first value in row contains the word "xyzk".
UltraEdit.activeDocument.findReplace.replace("^([^;\\r\\n]*xyzk.*;[^;\\r\\n]*)blah", "\\1xyzk");
// Replace word "blah" anywhere within second value of a row by "three".
UltraEdit.activeDocument.findReplace.replace("^(.*?;[^;\\r\\n]*)blah", "\\1three");
// Replace word "add" anywhere within third value of a row by "zpp".
UltraEdit.activeDocument.findReplace.replace("^(.*?;.*?;[^;\\r\\n]*)add", "\\1zpp");
// Replace word "odd" anywhere within third value of a row by "ipp".
UltraEdit.activeDocument.findReplace.replace("^(.*?;.*?;[^;\\r\\n]*)odd", "\\1ipp");
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: UltraEdit Script

Postby mane1034 » Fri Mar 08, 2013 3:24 am

Thanks Mofi.

Sorry I could not explain the requirements in detail.

With new code it seems everything is hardcoded, I could have unlimited number of different words which I need to replace with other characters.

There could be 1 to n possibilities of words and there could be n number of fields in each row and n number of lines

The exception words is a predefined list of words.

It does not matter how these characters are replaced.

See below input:

aadhaksjdh;asdasd;one;asdasd;asdasd
eat;asdasd;one;three,
sha;
blah.kra;
nnneed;
four
mane1034
Newbie
 
Posts: 5
Joined: Wed Mar 06, 2013 6:41 am

Re: UltraEdit Script

Postby Mofi » Fri Mar 08, 2013 6:53 am

Sorry, but I give it now up helping you. Every code in any language is based on clearly defined rules. If the rules are chaotic, it is not possible to write code for a task.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: UltraEdit Script

Postby mane1034 » Fri Mar 08, 2013 7:32 am

I can see your point.

Below I have tried to write down a high level procedure/process. I think here everything is pre-defined:

Read each individual line in the file (as there could be n number of lines in the file)
Then split each field world/field in the delimited file (as there could be n number of fields splitted by a seperator)
Compare each splitted field with the defined exception words list (one, two three etc) and if the splitted field is not matching with any of the word in the exception list then replace each character in the field based on below rule:

change character a to x
change character b to y
change character c to r
……
……
change character z to i

so if I have a file like:

a;aa;one
ba
ca;three
two;bb

then

I will get:

x;xx;one
yx
rx;three
two;yy


I think I need to explore how to parse a delimited file and then read each individual field.
mane1034
Newbie
 
Posts: 5
Joined: Wed Mar 06, 2013 6:41 am

Re: UltraEdit Script

Postby Mofi » Sun Mar 24, 2013 8:49 am

Here is a script based on the procedure guidelines you wrote in your previous post. It worked for the example in your previous post.

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.selectAll();   // Select the entire file.

   // Is anything selected, i.e. file is not empty?
   if (UltraEdit.activeDocument.isSel())
   {
      // Define the exception words in array with a counter.
      var asExceptionWords = {};
      asExceptionWords["one"] = 0;
      asExceptionWords["two"] = 0;
      asExceptionWords["three"] = 0;
      // Get all DOS terminated lines into an array of strings.
      var asLines = UltraEdit.activeDocument.selection.split("\r\n");

      // Processing all lines in the array.
      var nModifiedLines = nModifiedFields = 0;
      for (var nLine = 0; nLine < asLines.length; nLine++)
      {
         if (!asLines[nLine].length) continue;   // Skip all empty lines.

         // Split the line into fields at every semicolon.
         var asFields = asLines[nLine].split(";");
         var bFieldModified = false;

         for (var nField = 0; nField < asFields.length; nField++)
         {
            // Skip all empty fields, i.e. two semicolons in sequence.
            if (!asFields[nField].length) continue;
            // Does this field not contain an exception word?
            if (asExceptionWords[asFields[nField]] == null)
            {
               var sField = asFields[nField];
               // Make the character by character replaces.
               sField = sField.replace(/a/g,"x");
               sField = sField.replace(/b/g,"y");
               sField = sField.replace(/c/g,"r");
               sField = sField.replace(/z/g,"i");
               // Is the string of this field modified?
               if (sField != asFields[nField])
               {
                  asFields[nField] = sField;
                  bFieldModified = true;
                  nModifiedFields++;
               }
            }
            else  // Not nearly necessary but perhaps interesting
            {     // is which exception word was found how often.
               asExceptionWords[asFields[nField]]++;
            }
         }
         if (bFieldModified)  // Was any field in this line modified?
         {
            asLines[nLine] = asFields.join(";");
            nModifiedLines++;
         }
      }

      if (nModifiedLines)     // Was any line modified?
      {
         // Rebuild the block in user clipboard 9 and paste the
         // lines over the still selected lines in the active file.
         // Pasting a large block is much faster than writing.
         UltraEdit.selectClipboard(9);
         UltraEdit.clipboardContent = asLines.join("\r\n");
         UltraEdit.activeDocument.paste();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }

      // Move caret to top of file which cancels also the
      // selection in the case there was nothing modified.
      UltraEdit.activeDocument.top();

      // Output a summary in the output window.
      UltraEdit.outputWindow.showStatus = false;
      if (UltraEdit.outputWindow.visible == false)
      {
         UltraEdit.outputWindow.showWindow(true);
      }
      UltraEdit.outputWindow.clear();
      for (var sWord in asExceptionWords)
      {
         var nCount = asExceptionWords[sWord];
         UltraEdit.outputWindow.write("Exception word \""+sWord+"\" found "+nCount+" time"+((nCount==1) ? "." : "s."));
      }
      UltraEdit.outputWindow.write("\nSummary: "+nModifiedFields+" field"+((nModifiedFields==1) ? "" : "s")+
                                   " modified in "+nModifiedLines+" line"+((nModifiedLines==1) ? "." : "s."));
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts