Find/Replace one Value with another

Find/Replace one Value with another

3
NewbieNewbie
3

    Jun 08, 2023#1

    Hello,

    I am a super novice user with UltraEdit and currently am on Version UltraEdit Text/Hex Editor (x64) Version 24.20.0.51

    I have files where I need to search a specific column for a value and replace it with another value.
    I could use a macro but I would like to have a script for this as I am also splitting the file using a script - if i can incorporate them into one script it will be less confusing for users i need to share with.

    Example:  in the 123 byte/column there is a value of 'Z' - I need to check the entire column and replace any 'Z' with a space " ".

    I have tried searching the boards and have found similar functions but nothing that I am able to fully understand or see that it is actually checking for a specific column or group of columns.

    Can you advise on how I can do this?
    Thanks in advance :)
    Replace Value Z.PNG (4.03KiB)

    6,675585
    Grand MasterGrand Master
    6,675585

      Jun 09, 2023#2

      There are two possible solutions to replace the character Z by a space after the first 122 characters in a line.

      The first solution uses the replace in column feature.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         // The first line is for UE/UES for Windows and the second line for UE for Linux/Mac.
         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 the parameters for a case-sensitive replace in column 123 only using
         // a non-regular expression replace all and run the replace on entire 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.searchInColumn=true;
         UltraEdit.activeDocument.findReplace.fromCol=122;  // First column has column number 0.
         UltraEdit.activeDocument.findReplace.toCol=123;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace("Z", " ");
      }
      
      The second solution uses a Perl regular expression replace searching for lines with 122 characters at the beginning, keep them back which means do not select them for replace by using \K, and next character is an upper case Z which is replaced by a normal space character.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         // The first line is for UE/UES for Windows and the second line for UE for Linux/Mac.
         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 the parameters for a case-sensitive Perl regular expression
         // replace all and run the replace on entire file.
         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;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
         UltraEdit.activeDocument.findReplace.replace("^.{122}\\KZ", " ");
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Jun 09, 2023#3

        Thank you for this!

        I am attempting to run the first option now but its not doing anything - should I change anything within my environment or within this script prior to running?

        Thank you!

          Jun 09, 2023#4

          The second option worked great! I am sure I missed something weird on option 1.

          6,675585
          Grand MasterGrand Master
          6,675585

            Jun 09, 2023#5

            I tested both scripts on an example file created by myself and both scripts made the replacements using 32-bit UE v24.20.0.51 and v2023.0.0.41. I tested the first script with ANSI, UTF-8 and UTF-16 LE encoded text file with DOS and Unix line endings. The replace in column 123 worked for all file variants.

            If I should look for the reason why the first script does not work for you, please copy and paste some lines from file into a new file saved with same character encoding and same line ending format as the original file and if necessary obfuscate some data in the newly created file. Run the first script on this file and check if it still does not replace any Z by a space in column 123. If this is the case, compress the file into a ZIP file and post a reply with the ZIP file attached. Then I can look into it with using currently latest 2023.0.0.41 as well as with 24.20.0.51 restored from my archives.
            Best regards from an UC/UE/UES for Windows user from Austria