by Mofi » Thu May 04, 2006 6:18 am
The following macro will do the job. How it works:
First it makes sure that the last line of the file is terminated with a line ending character (CRLF for DOS, LF for Unix, CR for MAC). This is important for the macro or the result will not be what you want.
Next it moves the cursor to top and marks the start of every not blank line with the string "STARTOFLINE" because preceding white space characters should later not be removed by the macro.
Then the macro inserts at top of the file a line with a specified number of spaces - see red number.
Note: It's important that this number is higher than the longest line you have. Modify it if you have a line longer than 500 characters.
This line is now copied to bottom of the file. The cursor is now at the end of this line with spaces which is the longest line in the file and is always at end of the file. With SelectToTop in column mode now the whole file content is selected in column mode. With ColumnRightJustify now all lines are right justified and preceding spaces are added at each line of the file.
Next the added spaces on the last line are deleted and back at top of the file the longest line gets 1 character longer by inserting an additional space character.
A simple sort in ascending order with start column 1 sorts now the lines according to the line length because a shorter line has more preceding spaces as a longer line.
The green marked RemoveDup option removes blank lines and other duplicate lines. If you do not have blank lines, you can remove this option. If you have blank lines and do not use the RemoveDup option, all blank lines will be at top of the file after macro execution because the are the shortest lines.
To delete the blanks lines at top of the file if RemoveDup cannot be used, the blue colored loop can be used. Remove the blue colored code if you use RemoveDup or your file does not contain blank lines.
The longest line with only spaces is still at top of the file and is now removed.
Finally the preceding spaces added by ColumnRightJustify with the start of line marker string and also the trailing spaces are removed and you will hopefully have the result you want.
Note: Make sure your file does not contain hard tabs. Convert the tabs to spaces if the file contains tabs or the result will not look like you expect. You can do this conversion also with the macro command TabsToSpaces at start of the macro after command UnixReOff.
InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
TrimTrailingSpaces
Find RegExp "%^([~^r^n]^)"
Replace All "STARTOFLINE^1"
"
"
Key UP ARROW
Loop 500
" "
EndLoop
SelectToTop
Clipboard 9
Copy
Bottom
Paste
ClearClipboard
Clipboard 0
ColumnModeOn
SelectToTop
ColumnRightJustify
ColumnModeOff
EndSelect
Bottom
DeleteToStartofLine
Top
" "
SortAsc IgnoreCase RemoveDup 1 -1 0 0 0 0 0 0
DeleteLine
Loop
Find RegExp "% +^p"
Replace All ""
IfNotFound
ExitLoop
EndIf
EndLoop
Find MatchCase RegExp "% ++STARTOFLINE"
Replace All ""
TrimTrailingSpaces
Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.
Edited on 2008-02-06: Fixed macro to work correct also for large files with several thousands of lines. Previous version of the macro made the rectangular selection of whole file content in column mode wrong because of a bug in UltraEdit when the file is large.