Your first request can be fulfilled with a macro. The macro property
Continue if search string not found must be checked for this macro.
- Code: Select all
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Bottom
IfColNumGt 1
InsertLine
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
Top
TrimTrailingSpaces
Find MatchCase RegExp "[ ^t]+"
Replace All "#"
Find MatchCase RegExp "%^([0-9]+^)$"
Replace All "#^1"
Find MatchCase RegExp "%^([0-9]+#^)"
Replace All "#^1"
Find MatchCase RegExp "^([0-9]^)#"
Replace All "^1^p#"
Loop 0
Find MatchCase "#"
IfNotFound
ExitLoop
EndIf
Key LEFT ARROW
StartSelect
Find MatchCase RegExp Select "%[~#^p]"
IfFound
Key LEFT ARROW
EndSelect
SortAsc Numeric 2 -1 0 0 0 0 0 0
Find MatchCase RegExp "%[0-9]"
IfNotFound
ExitLoop
EndIf
Else
SelectToBottom
SortAsc Numeric 2 -1 0 0 0 0 0 0
ExitLoop
EndIf
EndLoop
Top
Find MatchCase "#"
Replace All ""
Here is the macro again with indentations and comments for better understanding how it works. You should save that into a text file with same name as the macro file *.mac containing the compiled macro code, but with file extension
uem (or txt).
- Code: Select all
// Define the environment for the macro.
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
// Make sure that also last line of file ends with a line termination.
Bottom
IfColNumGt 1
InsertLine
IfColNumGt 1
DeleteToStartofLine
EndIf
EndIf
// Back to top of the file and delete all trailing spaces.
Top
TrimTrailingSpaces
// Find strings with 1 or more spaces / tabs and replace
// each of them by character #.
Find MatchCase RegExp "[ ^t]+"
Replace All "#"
// Find lines containing only a number and nothing else
// and insert the character # at beginning of such lines.
Find MatchCase RegExp "%^([0-9]+^)$"
Replace All "#^1"
// Find lines starting with a number followed by character #
// and insert the character # at beginning of such lines.
Find MatchCase RegExp "%^([0-9]+#^)"
Replace All "#^1"
// Find a digit followed by character # and insert a line
// termination after the digit.
Find MatchCase RegExp "^([0-9]^)#"
Replace All "^1^p#"
// Now every number of a day is on a separate line with # at beginning.
// Run the following loop until the numbers of all days are sorted.
Loop 0
// Find next line with character # at beginning. If not found, exit loop.
Find MatchCase "#"
IfNotFound
ExitLoop
EndIf
// Move caret left to found character # and start selection mode.
Key LEFT ARROW
StartSelect
// Select everything up to a line not starting with character # (= next day).
Find MatchCase RegExp Select "%[~#^p]"
IfFound
// If such a line could be found, move the caret left to beginning of line
// while selection mode is still active. Now all number lines of a day are
// selected. Sort this block which results in discarding the selection and
// moving the caret to start of the first line of the block. So use another
// regular expression Find to move caret downwards to the first line of the
// next day (starts with a digit).
Key LEFT ARROW
EndSelect
SortAsc Numeric 2 -1 0 0 0 0 0 0
Find MatchCase RegExp "%[0-9]"
IfNotFound
ExitLoop
EndIf
Else
// If there is no line not starting with character # anymore, this is the
// last day in the file. Select from start of first line with a number to
// end of line and sort this last block before exiting the loop. This is
// the typical exit. The other 2 exits are just for security in case the
// macro is executed on a wrong file.
SelectToBottom
SortAsc Numeric 2 -1 0 0 0 0 0 0
ExitLoop
EndIf
EndLoop
// Move caret to top of file and delete all inserted # from the file.
Top
Find MatchCase "#"
Replace All ""
Your second request can't be done with an UltraEdit macro. That requires an UltraEdit script with support for variables and arrays.