User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and playing macros
2 posts Page 1 of 1
I want to renumber only the lines with text and not 8) number blank lines. Is there any hope for me? I search for lines with the letter N and numbers followed by spaces and delete them until I get to the end of the file. Then renumber the entire file.

Code: Select all
InsertMode
ColumnModeOn
HexOff
UltraEditReOn
Top
Loop 0
Find RegExp "[N][0-9]+[ ]+"
IfNotFound
ExitLoop
Else
Delete
EndIf
EndLoop
Top
ColumnInsert " "
Top
ColumnInsert "N"
Top
Key RIGHT ARROW
ColumnInsertNum 1000 10
For small files I would do this with a script which loads entire file into memory, modifies there the beginning of the lines and outputs all lines at once overwriting entire file content. This would have the advantage of producing only one undo record.

However, here is a macro solution for this task in style required to copy the code into Edit Macro dialog with property Continue if search string not found being enabled for this macro. The macro is written for files with DOS/Windows line terminators. Replace all ^p by ^n in macro for UNIX files not converted to DOS on load.

First number and increment can be of course modified in macro code, too.

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
TrimTrailingSpaces
Clipboard 9
ClearClipboard
IfCharIs 13
Find MatchCase RegExp "[^r^n]+"
Cut
EndIf
IfCharIs 10
Find MatchCase RegExp "[^r^n]+"
Cut
EndIf
"#"
Bottom
Loop 0
IfColNum 1
Key BACKSPACE
Else
ExitLoop
EndIf
EndLoop
Top
Delete
Find MatchCase RegExp "%N[0-9]+ +"
Replace All ""
Loop 0
Find MatchCase RegExp "^p$"
Replace All "^pBlAnKlInE"
IfNotFound
ExitLoop
EndIf
EndLoop
Find MatchCase "^pBlAnKlInE"
Replace All "BlAnKlInE"
ColumnModeOn
ColumnInsert "N "
Key RIGHT ARROW
ColumnInsertNum 1 1
ColumnModeOff
Bottom
InsertLine
Top
Find MatchCase RegExp "N^( +^)^([0-9]+^)"
Replace All "N^2^1"
Find MatchCase "BlAnKlInE"
Replace All "^p"
Paste
ClearClipboard
Clipboard 0
Top

Here is the same code again, but with indentations and comments for better understanding the code:

Code: Select all
//  Delete all trailing spaces/tabs from all lines to convert all blank lines
//  to empty lines. For details see the post "Remove / delete blank and empty
//  lines" at https://www.ultraedit.com/forums/viewtopic.php?f=8&t=13703#p48039
    InsertMode
    ColumnModeOff
    HexOff
    UnixReOff
    Top
    TrimTrailingSpaces

//  Cut all blank lines at beginning of file to user clipboard 9 if there are blank lines.
    Clipboard 9
    ClearClipboard
    IfCharIs 13
        Find MatchCase RegExp "[^r^n]+"
        Cut
    EndIf
    IfCharIs 10
        Find MatchCase RegExp "[^r^n]+"
        Cut
    EndIf

//  Insert at top a character to make sure the loop below does not result
//  in an endless running loop if this macro is executed on an empty file.
    "#"

//  Delete all line terminators at end of file.
    Bottom
    Loop 0
        IfColNum 1
            Key BACKSPACE
        Else
            ExitLoop
        EndIf
    EndLoop

//  Delete the inserted character at top of file.
    Top
    Delete

//  Delete all already existing line numbers at beginning of all
//  lines after character N and also all spaces after each number.
    Find MatchCase RegExp "%N[0-9]+ +"
    Replace All ""

//  Insert the uncommon written and therefore most likely never existing
//  string BlAnKlInE on every blank line. This must be done in a loop as
//  there can be multiple blank lines in series.
    Loop 0
        Find MatchCase RegExp "^p$"
        Replace All "^pBlAnKlInE"
        IfNotFound
            ExitLoop
        EndIf
    EndLoop

//  Remove the line terminator before every previously blank line.
    Find MatchCase "^pBlAnKlInE"
    Replace All "BlAnKlInE"

//  Insert at beginning of each line character N and a single space.
    ColumnModeOn
    ColumnInsert "N "

//  Insert after character N the incrementing decimal number
//  starting with value 1 and incrementing on each line by 1.
    Key RIGHT ARROW
    ColumnInsertNum 1 1
    ColumnModeOff

//  Move to end of file and insert a line termination.
    Bottom
    InsertLine

//  Back at top of file move all spaces left to inserted number right
//  to the number in case of left alignment is wanted for the numbers.
    Top
    Find MatchCase RegExp "N^( +^)^([0-9]+^)"
    Replace All "N^2^1"

//  Restore the blank lines with a simple replace.
    Find MatchCase "BlAnKlInE"
    Replace All "^p"

//  Restore the blank lines at top of the file.
    Paste
    ClearClipboard
    Clipboard 0
    Top
Best regards from Austria
2 posts Page 1 of 1
cron