How to insert a text into a specific column on every selected line?

How to insert a text into a specific column on every selected line?

8
NewbieNewbie
8

    Jul 02, 2008#1

    I searched around for an answer to this, but couldn't find it which surprised me. So if this has been posted before, please point me in the right direction and I'll read up.

    I'm wanting to select a block of text, and then perform a specific action to each line of that selection, specifically, I'm wanting to place the same string of text in specific columns of each line.

    For example:

    Code: Select all

    Here is some text
       I've got some here too
     Oh Look a 3rd line
     4th line is best
    Would end up looking like this:

    Code: Select all

    Here is some text                    A0001
       I've got some here too            A0001
     Oh Look a 3rd line                   A0001
     4th line is best                     A0001
    I know how to grab the text from the user, but I'm unsure how to perform the action on ONLY the selected lines of text.

    6,686585
    Grand MasterGrand Master
    6,686585

      Jul 03, 2008#2

      Following script which uses 2 replaces all in selected text works for your example. If your example was not as real data looks like, you have to adapt it by yourself. In your example all selected lines are shorter than after script execution. So the first replace all in selected text simply inserts the 37 spaces and "A0001" before every DOS line termination. The second regular expression replace all in still selected text in UltraEdit syntax lets the first 37 characters of every line in the selection unchanged, but removes all following spaces to "A0001". For UNIX files use ^n instead of ^p in the search and replace strings. The script was tested on your example with UE v14.10.

      Code: Select all

      if (UltraEdit.document.length > 0)
      {
         if (UltraEdit.activeDocument.isSel()) {
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            UltraEdit.activeDocument.hexOff();
            UltraEdit.ueReOn();
            UltraEdit.activeDocument.findReplace.matchCase=false;
            UltraEdit.activeDocument.findReplace.matchWord=false;
            UltraEdit.activeDocument.findReplace.preserveCase=false;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.replaceAll=true;
            UltraEdit.activeDocument.findReplace.selectText=true;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            UltraEdit.activeDocument.findReplace.replace("^p", "                                     A0001^p");
            UltraEdit.activeDocument.findReplace.regExp=true;
            UltraEdit.activeDocument.findReplace.replace("%^(?????????????????????????????????????^) +A0001", "^1A0001");
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      8
      NewbieNewbie
      8

        Jul 03, 2008#3

        Well, more specifically to my need, I actually want the text to be inserted into a specific column regardless of what else is on the line.

        I'm wanting to insert the text "A0001" into column 67 of every line within the selection.

        6,686585
        Grand MasterGrand Master
        6,686585

          Jul 06, 2008#4

          Normally such a text inserting is done manually by activating the column mode, for example with Alt+C, selecting the lines at the column where the text should be inserted, simply type the text or use Column - Insert/Fill Columns and turn off column mode by pressing again Alt+C. I normally need less than 5-10 seconds for such a text inserting.

          But maybe you want to do it with a script because it is only a part of other actions you do with a script. Because I still don't know your version of UltraEdit and the regular expression engine you prefer, I developed a solution which works without any find/replace operation.

          The advantage of this solution is that you can undo the changes by the script with a single Ctrl+Z (one undo step) because the source file is modified only by one paste command.

          The disadvantages are that it is not the fastest method and it can destroy data if the source file is a Unicode file, but new files are created by default as ASCII. You would need extra script code to avoid wrong data copying between the source file and the new file which is temporarily used in that case. Also this method could insert the text in the wrong column (68 instead of 67) depending on your version of UltraEdit. I have not added the workaround code for the affected versions of UltraEdit which set the cursor in the first line of a Unicode file with BOM to the wrong column because it counts the invisible BOM as character in the line too. However, I guess your files are ASCII/ANSI files and so the simplest version will be enough.

          You have to insert the function getActiveDocumentIndex at top of this script to be able to use it. I tested this script with UE v14.00b+1 with some examples I created myself.

          Code: Select all

          if (UltraEdit.document.length > 0)
          {
             if (UltraEdit.activeDocument.isSel()) {
                UltraEdit.insertMode();
                if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                UltraEdit.activeDocument.hexOff();
                UltraEdit.selectClipboard(9);
                UltraEdit.activeDocument.copy();
                var iActiveIndex = getActiveDocumentIndex();
                UltraEdit.newFile();
                UltraEdit.activeDocument.paste();
                var bLastLineTerminated = false;
                if (UltraEdit.activeDocument.isColNum(1)) bLastLineTerminated = true;
                UltraEdit.activeDocument.top();
                UltraEdit.activeDocument.key("END");
                if (UltraEdit.activeDocument.isColNumGt(66)) {
                   UltraEdit.activeDocument.gotoLine(1,67);
                } else {
                   do UltraEdit.activeDocument.write(" ");
                   while (!UltraEdit.activeDocument.isColNum(67))
                }
                if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
                else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
                UltraEdit.activeDocument.columnInsert("A0001");
                if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                if (bLastLineTerminated) {
                   UltraEdit.activeDocument.bottom();
                   UltraEdit.activeDocument.deleteToStartOfLine();
                }
                UltraEdit.activeDocument.selectAll();
                UltraEdit.activeDocument.copy();
                UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                UltraEdit.document[iActiveIndex].setActive();
                UltraEdit.activeDocument.paste();
                UltraEdit.clearClipboard();
                UltraEdit.selectClipboard(0);
             }
          }
          Best regards from an UC/UE/UES for Windows user from Austria

          8
          NewbieNewbie
          8

            Jul 09, 2008#5

            I truly do apologize for wasting any of your time. And I do appreciate the fact that you took the time to develop a script to do exactly what I wanted to do, however this was not my intention. It would have sufficed for you to simply explain the method by which to achieve my goal rather than spending your free time developing it.

            Again... thank you for your solution, as it does exactly what I wanted!