Find & save to file

Find & save to file

12
Basic UserBasic User
12

    Sep 08, 2005#1

    Hi,

    I need to find a specific string in one big text file and save it to a new file.
    Below are the sample of the text.

    1.0 abc
    1.5 def
    2.8 abd
    2.7 bfv
    2.9 bbg
    2.0 bbh
    2.2 ffg
    2.1 ddf
    3.4 jjj
    3.2 hhh
    3.9 lll
    4.5 kkk

    What I need to do;

    All strings that start with 1.XXX will be saved as 001.txt
    All strings that start with 2.XXX will be saved as 002.txt
    All strings that start with 3.XXX will be saved as 003.txt
    ...

    Pls advise on how to create the macro.

    Thanks,

      Sep 09, 2005#2

      Hi,

      Below are my attempt on creating the Macro;

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOn
      Top
      Find RegExp "^[2]\.[0-9]*.*"
      Copy
      Paste
      NewFile
      SaveAs "002.txt"

      Problems are;

      1. I tried the RegExp via Find function and it works well, so I guess the codes are correct.
      2. I run the Macro only to find out that it just created the 002.txt with no text inside it. Should I use a Loop for it? I not sure how to do it actually.

      Pls help.

      Thank you so much.

      6,675585
      Grand MasterGrand Master
      6,675585

        Sep 09, 2005#3

        Your example expanded (not tested):

        InsertMode
        ColumnModeOff
        HexOff
        UnixReOn
        Top
        Clipboard 9
        ClearClipboard
        Loop
        Find RegExp "^[2]\.[0-9]*.*$"
        IfFound
        CopyAppend
        Else
        ExitLoop
        EndIf
        EndLoop
        NewFile
        Paste
        SaveAs "002.txt"
        ClearClipboard
        Clipboard 0
        Best regards from an UC/UE/UES for Windows user from Austria

        12
        Basic UserBasic User
        12

          Sep 10, 2005#4

          Thanks Mofi,

          I tried to run the Macro using the sample text given earlier but got error "Search string not found". No text file created also.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOn
          Top
          Clipboard 9
          ClearClipboard
          Loop
          Find RegExp "^[2]\.[0-9]*.*$"
          IfFound
          CopyAppend
          Else
          ExitLoop
          EndIf
          EndLoop
          NewFile
          Paste
          SaveAs "c:\002.txt" <-- is this correct?
          ClearClipboard
          Clipboard 0

          Note that I have added the path in the SaveAs portion(in bold).

          Questions;
          How do I process for the rest of the content?
          Do I have to copy paste each code. Example below;

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOn
          Top
          Clipboard 9
          ClearClipboard
          Loop
          Find RegExp "^[2]\.[0-9]*.*$"
          IfFound
          CopyAppend
          Else
          ExitLoop
          EndIf
          EndLoop
          NewFile
          Paste
          SaveAs "c:\002.txt"
          ClearClipboard
          Clipboard 0
          Top
          Clipboard 9
          ClearClipboard
          Loop
          Find RegExp "^[3]\.[0-9]*.*$"
          IfFound
          CopyAppend
          Else
          ExitLoop
          EndIf
          EndLoop
          NewFile
          Paste
          SaveAs "c:\003.txt"
          ClearClipboard
          Clipboard 0
          Top
          Clipboard 9
          ClearClipboard
          Loop
          Find RegExp "^[4]\.[0-9]*.*$"
          IfFound
          CopyAppend
          Else
          ExitLoop
          EndIf
          EndLoop
          NewFile
          Paste
          SaveAs "c:\004.txt"
          ClearClipboard
          Clipboard 0

          ....

          Is there a much easier way? I notice there is a character ^c to for clipboard, can we use this?

          Pls advise.

          Thanks.

          6,675585
          Grand MasterGrand Master
          6,675585

            Sep 11, 2005#5

            The path specification in the SaveAs command is correct (only one '\').

            I now tested it and replaced the $ at end of the regular expression search by a \p to avoid that all found strings are stored in a single line. And I removed the square brackets around the first number, because they are simply not necessary.

            Activate also the macro property Continue if a Find with Replace not found for your macro.

            What you also have forgotten is the CloseFile command. After first search loop and SaveAs the next search loop was running at the already saved new file and so nothing was found.

            Last I also added with SelectAll and IfSel commands a detection, if the new file really contains anything. If not the new file is closed without saving it to avoid creation of files without content (0 byte file size).

            Here is the modified macro:

            InsertMode
            ColumnModeOff
            HexOff
            UnixReOn
            Clipboard 9
            Top
            ClearClipboard
            Loop
            Find RegExp "^
            1\.[0-9]*.*\p"
            IfFound
            CopyAppend
            Else
            ExitLoop
            EndIf
            EndLoop
            NewFile
            Paste
            SelectAll
            IfSel
            SaveAs "c:\00
            1.txt"
            EndIf
            CloseFile NoSave

            ... copy green block also for numbers 2 to 9 here ...
            Top
            ClearClipboard
            Clipboard 0


            Although ^c could be used for Find and SaveAs, this would unnecessarily complicate your macro for your example. Using ^c would need a modification of the source file to temporary remove all already found lines by using CutAppend instead of CopyAppend. Also it would be necessary to have a nested loop, which is not possible in UltraEdit's macro language, so the inner loop must be put in an extra macro. I know how to write this 2 macros, but it is not necessary to do that until the number at starting of each line before the point is not increasing.
            Best regards from an UC/UE/UES for Windows user from Austria

            7
            NewbieNewbie
            7

              Jan 05, 2008#6

              I tried the above and it only copied exactly the search string though into the file.. not the line on which it was found.

              What I am searching is a file that contains thousands of lines like the following:

              NUM: 9030
              SEV: 2
              TXT: TLSX is in error \E, correct and restart

              Plus a bunch of other additional lines in the file, not just these lines.

              I optimally want to turn this into a CSV file that looks like the following for each entry.

              9030,2,TLSX is in error \E, correct and restart

              The Macro I currently have is (which right now just copies "NUM:" into the file.
              ----
              InsertMode
              ColumnModeOff
              HexOff
              UnixReOn
              Clipboard 9
              Top
              ClearClipboard
              Loop
              Find MatchCase "NUM:"
              IfFound
              CopyAppend
              Else
              ExitLoop
              EndIf
              EndLoop
              NewFile
              Paste
              SelectAll
              IfSel
              SaveAs "C:\CRSTMP\UETmp.txt"
              EndIf
              CloseFile NoSave
              ClearClipboard
              Clipboard 0
              ---

              Christopher

              6,675585
              Grand MasterGrand Master
              6,675585

                Jan 05, 2008#7

                Hi Christopher,

                you need to select the entire line(s) with a regular expression search or only the searched word is selected and copied. Here is the macro you will need to get the CSV file.

                The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

                InsertMode
                ColumnModeOff
                HexOff
                UnixReOn
                Clipboard 9
                Bottom
                IfColNum 1
                Else
                "
                "
                EndIf
                Top
                ClearClipboard
                Loop
                Find MatchCase RegExp "^NUM:.*\pSEV:.*\pTXT:.*\p"
                IfFound
                CopyAppend
                Else
                ExitLoop
                EndIf
                EndLoop
                NewFile
                Paste
                Top
                Find MatchCase RegExp "^NUM:\s*(.*)\pSEV:\s*(.*)\pTXT:\s*"
                Replace All "\1,\2,"
                IfFound
                SaveAs "C:\CRSTMP\UETmp.txt"
                EndIf
                ClearClipboard
                Clipboard 0

                Add UnixReOff or PerlReOn (v12+ of UE) at the end of the macro if you do not use Unix style regular expressions by default - see search configuration. Macro command UnixReOn sets the regular expression option to Unix style.
                Best regards from an UC/UE/UES for Windows user from Austria

                7
                NewbieNewbie
                7

                  Jan 07, 2008#8

                  Sweet! Thanks that worked like a charm and did everything I needed it to!

                  Thanks a lot!

                  Christopher