How to insert line termination after every X number of characters?

How to insert line termination after every X number of characters?

3

    Aug 07, 2019#1

    Hello!

    I'm working on a hobby project and I need a program that can auto-magically set line termination every X number of characters.

    Does UltraEdit have this feature? If not, can anyone suggest an alternative?

    In case anyone is curious, the files I'm working with are actually hex code copied and pasted from the gamdb file from the game FEAR. I want to compare 2 slightly different hex codes of the same file type in Diff Merge or something similar to see which lines are altered between the 2 so I'll know which/what to edit in a third file.

    When I paste paste raw hex it comes out as a single line. I need something that can auto-magically format it the same way it is in a hex editor.

    61
    NewbieNewbie
    61

      Aug 08, 2019#2

      You can use a script, for example:

      Code: Select all

      //query x and insert eol after each x bytes (in ascii file)
      
      if( UltraEdit.activeDocument.encoding == 0 )
      {
        var len = UltraEdit.getValue("Number of bytes per line?",1);
        if( len > 1 )
        {
          var eol = "\r\n";
          switch( UltraEdit.activeDocument.lineTerminator )
          {
            case 1: eol = "\n"; break;
            case 2: eol = "\r"; break;
          }
          UltraEdit.activeDocument.selectAll();
          var istr = UltraEdit.activeDocument.selection.replace(/[\r\n]/g, "");
          var ostr = "";
          for( var i = len; i < istr.length; i += len )
          {
            ostr += istr.slice(i-len,i)+eol;
          }
          UltraEdit.activeDocument.write(ostr + istr.slice(i-len,i));
        }
      }
      else if( !UltraEdit.activeDocument.hexMode )
      {
        UltraEdit.messageBox("ANSI encoding required.")
      }

      3

        Aug 08, 2019#3

        Thank you for your response bersch and for taking the time to come up with the script. However, it seems I don't actually need it.

        After I downloaded UltraEdit I was exploring the tool bar at the top and noticed you could format the text after pasting it into the editor by column number (add line returns at column #). This is all I needed. Again though, thank you for offering the script. I have it saved on my desktop and who knows, it may prove useful sometime.

        Side note: I really like UltraEdit. Very slick interface, nice features, I think I might purchase it after my next payday. ;)

        6,626551
        Grand MasterGrand Master
        6,626551

          Aug 08, 2019#4

          UltraEdit has also on ribbon tab Edit in popup menu of item Insert item the command String at every increment which can be used to insert ^p (means carriage return + line feed) after every X characters of a file.

          It is of course also possible to use a Perl regular expression replace with search string (?s).{X}\K and replace string \r\n to insert every X characters (must be a number) carriage return + line feed. (?s) at beginning is just for matching also a carriage return and a line feed as characters by the dot. If the file does not contain any carriage return or line feed, this flag controlling matching behavior of the dot is not needed on running the Perl regular expression replace.

          It is also possible to configure UltraEdit to insert CR+LF after column X automatically on opening a file with a specific file extension. Then you would only need to save the file modified already on file open with Ctrl+S. See forum topic File extension based word wrap, tab and indent settings.

          BTW: Have you ever tried smart binary comparison of UltraCompare?
          Best regards from an UC/UE/UES for Windows user from Austria

          3

            Aug 09, 2019#5

            Thank you for the info. Particularly the last paragraph, ;)

            No, I don't know what smart binary comparison is, but it sure sounds clever! :D

            I've only tried the normal text compare so far, just for quick test to see whether or not it would crash, hehe. DiffMerge was crashing/freezing constantly dealing with large files and WinMerge lacked the capabilities I was looking for.

            I'm impressed with UltraCompare so far, I must say.