Macro to split a large file and save the new ones with their first word as a filename

Macro to split a large file and save the new ones with their first word as a filename

2

    Jan 13, 2015#1

    Is there a way to make a macro that will automatically split a large file into smaller ones when it encounters a specific string (I have made this work) and then save all the new files to a specified fixed location, using as a filename the first word of each file, trimmed from whitespaces?

    6,686585
    Grand MasterGrand Master
    6,686585

      Jan 13, 2015#2

      Of course, this can be done with an UltraEdit macro. Better would be an UltraEdit scripting solution as macros do not support variables, but variables are supported by scripts. Therefore coding a script would be here most likely easier than coding a macro.

      But your explanation is a little bit poor. It would be good to show us with a block what active file contains on execution of macro/script and what should be produced by the macro/script. It would be enough to post a block of about 20-30 lines enclosed in code tags (button above edit field) showing active file contents and 2-3 "code" blocks showing what the created files should contain and which names the files should have.
      non-serviam wrote:using as a filename the first word of each file, trimmed from whitespaces
      A word cannot contain spaces. So this statement is not clear for me at all.
      Best regards from an UC/UE/UES for Windows user from Austria

      2

        Jan 14, 2015#3

        Mofi wrote:A word cannot contain spaces. So this statement is not clear for me at all.
        You are right. I meant a title of two words maximum without the whitespace between them.

        The format of the original file is:

        Original.txt

        Code: Select all

        title1
        stuff1
        stuff1
        stuff1
        end word
        
        title2
        stuff2
        stuff2
        stuff2
        end word
        
        title3
        stuff3
        stuff3
        stuff3
        end word
        And I would like to get:

        title1.txt

        Code: Select all

        title1
        stuff1
        stuff1
        stuff1
        end word
        title2.txt

        Code: Select all

        title2
        stuff2
        stuff2
        stuff2
        end word
        title3.txt

        Code: Select all

        title3
        stuff3
        stuff3
        stuff3
        end word

        6,686585
        Grand MasterGrand Master
        6,686585

          Jan 14, 2015#4

          Okay, here is the code for an UltraEdit macro working on your example data with UE v21.30.0.1016.

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          PerlReOn
          Top
          Clipboard 9
          Loop 0
          StartSelect
          Find MatchCase Select "end word"
          IfSel
          Key HOME
          Copy
          EndSelect
          Key END
          NewFile
          Paste
          Top
          Find RegExp "[a-z]\w*([ \t]+\w+)?"
          IfFound
          Copy
          Top
          Paste
          "
          "
          Top
          SelectLine
          Find MatchCase RegExp SelectText "[ \t]+"
          Replace All ""
          Top
          StartSelect
          Key END
          Copy
          EndSelect
          DeleteLine
          SaveAs "^c.txt"
          Else
          ClearClipboard
          SaveAs ""
          EndIf
          CloseFile NoSave
          Find MatchCase RegExp "^(?![\r\n])"
          Else
          ExitLoop
          EndIf
          EndLoop
          ClearClipboard
          Clipboard 0
          And here is the same code again indented and with comments for storing as text backup of binary macro and better understanding.

          I suggest to use file extension uem and use the syntax highlighting wordfile for text version of UltraEdit macros.

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          PerlReOn
          Top
          Clipboard 9
          Loop
          //  Find the keyphrase and select everything from current
          //  position of caret to end of searched and found string.
              StartSelect
              Find MatchCase Select "end word"
          //  Was anything found and is therefore something selected?
              IfSel
          //  Move caret to beginning of the line with the keyphrase.
                  Key HOME
                  IfColNumGt 1
                      Key HOME
                  EndIf
          //  Copy the selection. There is hopefully still something selected.
          //  Then cancel selection and move caret to end of line with keyphrase.
                  Copy
                  EndSelect
                  Key END
          //  Create a new file, paste copied text, move caret to top and search
          //  for a word starting with a letter in any case and optionally a
          //  second word after 1 or more spaces/tabs.
                  NewFile
                  Paste
                  Top
                  Find RegExp "[a-z]\w*([ \t]+\w+)?"
          //  Could one or two words be found in entire text?
                  IfFound
          //  Copy those one or two words into a new line at top of new file.
                      Copy
                      Top
                      Paste
                      "
                      "
                      Top
          //  Select this line and run on this selection only a regular expression
          //  replace to remove all spaces and tabs if two words were found before.
                      SelectLine
                      Find MatchCase RegExp SelectText "[ \t]+"
                      Replace All ""
          //  Reselect the remaining sequence of letters, digits and underscores,
          //  copy them to clipboard, delete the inserted line and save the new
          //  file in current working directory with appropriate name.
                      Top
                      StartSelect
                      Key END
                      Copy
                      EndSelect
                      DeleteLine
                      SaveAs "^c.txt"
                  Else
          //  In case of not at least 1 word could be found starting with a letter,
          //  let the user of the macro enter the file name on saving new file. The
          //  macro user can also cancel saving this new file with strange content.
                      ClearClipboard
                      SaveAs ""
                  EndIf
          //  Close the new file.
                  CloseFile NoSave
          //  Move caret on next none blank line in file to split.
                  Find MatchCase RegExp "^(?![\r\n])"
              Else
                  ExitLoop
              EndIf
          EndLoop
          // Clear used user clipboard 9 and switch back to system clipboard.
          ClearClipboard
          Clipboard 0
          Best regards from an UC/UE/UES for Windows user from Austria