Delete line with partially duplicate content

Delete line with partially duplicate content

2
NewbieNewbie
2

    Apr 05, 2006#1

    Hello,

    I try to create a macro that searches a file for multiple entries, and deletes all that start with a duplicate number (all but the first line).
    The lines always start with 7 digits followed by a semicolon. This number is what I want to search for.

    0019608;HIRSCH-A
    0020416;MARIEN A
    0023403;ADLER AP
    0023403;ADLER-AP
    0024012;BÄREN-AP
    0024012;BÄREN-AP
    0024012;BÄREN-AP
    0024940;OBERE ST
    0024940;OSA;DILL

    the result should look like this:

    0019608;HIRSCH-A
    0020416;MARIEN A
    0023403;ADLER AP
    0024012;BÄREN-AP
    0024940;OBERE ST

    sadly I don't know anything about UltraEdit Macros so I tried to created one with copying parts of macros from this forum, but it won't work..

    How can I do this? :)
    Thanks in advance
    jrpm

    6,675585
    Grand MasterGrand Master
    6,675585

      Apr 05, 2006#2

      No problem. This macro needs the macro property Continue if a Find with Replace not found.

      Please also look at the multiple lines with 0024012 after macro execution. I know that old V10.10c of UE does not corretly delete all these lines with a singe replace all. You have to remove the % character in the regular expression search string (= start of the line) if it not really works with your version of UE. Or you create a submacro for the find/replace command only which runs the replace in a loop until IfNotFound is true. I will help you with the bug workaround macros if this macro does not work correctly with your version of UE.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Clipboard 9
      Bottom
      IfColNum 1
      Else
      "
      "
      EndIf
      Top
      Loop
      IfEof
      ExitLoop
      EndIf
      SelectWord
      StartSelect
      Copy
      EndSelect
      Key HOME
      Key DOWN ARROW
      Find RegExp "%^c;*^p"
      Replace All ""
      EndLoop
      ClearClipboard
      Clipboard 0

      Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style. This job can be done only with an UltraEdit style regular expression without a really big workaround.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Apr 06, 2006#3

        Thank you, it worked!

        5
        NewbieNewbie
        5

          Nov 30, 2006#4

          Mofi,
          Thank you contributing this code. It makes sense but when I run within UE 12.2, it deletes the entire file. It seems the problem is the Replace All "" command which seems to wrap around the file. I have the parameter "Continue find at End of File" in my preferences unchecked.
          I did a Macro Quick Record and within my Find & Replace dialog varied the function once with a check in "Replace All is From Top of File" which replicated the problem with the Macro and another time with out the check and that worked predictably. But if you look at the code generated by the quick record command there is nothing to differentiate the two functions:

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          Find RegExp "%^c|*^p"
          Replace All ""
          StartSelect
          Copy 
          Find RegExp "%^c|*^p"
          Replace All ""
          Thank you

          6,675585
          Grand MasterGrand Master
          6,675585

            Nov 30, 2006#5

            This also happens with UE v11.20b. Reason: option Replace All is From Top of File is a replace dialog only option which is not available as macro command parameter. But the option from the replace dialog is active also while running a macro which should not be.

            You should report this issue by email to IDM support. The replace option Replace All is From Top of File should be ignored when a replace all from within a macro is executed.

            It's clear that the macro with this option active will produce an empty file. The macro sets the cursor once to top of the file before the loop and then runs the replaces in the loop linewise until end of file is reached. With this option the cursor is set within the loop always to top of the file and so the loop will only exit when everything is deleted. I personally never check Replace All is From Top of File and so I did not notice this bad effect before your post.
            Best regards from an UC/UE/UES for Windows user from Austria

            5
            NewbieNewbie
            5

              Nov 30, 2006#6

              Thank you Mofi, I have notified UE support.
              Do you have any other coding ideas on how I could eliminate duplicate records with a common cust number in the first field?
              Thank you

              6,675585
              Grand MasterGrand Master
              6,675585

                Nov 30, 2006#7

                Same macro as above without replace all command and so much slower and more complicated, but independent now from the special replace all option. The replace is now executed one by one in the loop. A bookmark is used to go back to initial start position. So the macro first clears all bookmarks before it really starts.

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

                InsertMode
                ColumnModeOff
                HexOff
                UnixReOff
                Clipboard 9
                Bottom
                IfColNum 1
                Else
                "
                "
                EndIf
                Loop
                PreviousBookmark
                IfEof
                ExitLoop
                Else
                ToggleBookmark
                Bottom
                EndIf
                EndLoop
                Top
                SelectWord
                StartSelect
                Copy
                EndSelect
                Key HOME
                ToggleBookmark
                Key DOWN ARROW
                Loop
                Find RegExp "%^c|*^p"
                IfFound
                Delete
                Else
                PreviousBookmark
                ToggleBookmark
                Key DOWN ARROW
                IfEof
                ExitLoop
                EndIf
                SelectWord
                StartSelect
                Copy
                EndSelect
                Key HOME
                ToggleBookmark
                Key DOWN ARROW
                EndIf
                EndLoop
                ClearClipboard
                Clipboard 0
                Best regards from an UC/UE/UES for Windows user from Austria