Anyone know how to pad an entire text file so that each line length is uniform?
Thanks,
Matt
Line Padding
Moderators: AmigoJack, bbadmin, helios, MudGuard
Line Padding - options
Here is one method.
Use regular expression to add a ridiculous amount of spaces to the end of all lines (at least as many as your desired line lenth):
search string:
$
replace string:
&<then hold down the space bar>
Once this is done you can trim each line with:
search string:
^([^%]{100}).*$
replace string:
\1
Replace "100" with the desired string length.
I have posix enabled with this, otherwise you will have to escape the curly brackets.
Also, the percent sign should be replaced with a unique character that is not present in any of the text strings (if a percent sign is present). a simple presearch will tell you if it is.
Regards,
Kenton
Use regular expression to add a ridiculous amount of spaces to the end of all lines (at least as many as your desired line lenth):
search string:
$
replace string:
&<then hold down the space bar>
Once this is done you can trim each line with:
search string:
^([^%]{100}).*$
replace string:
\1
Replace "100" with the desired string length.
I have posix enabled with this, otherwise you will have to escape the curly brackets.
Also, the percent sign should be replaced with a unique character that is not present in any of the text strings (if a percent sign is present). a simple presearch will tell you if it is.
Regards,
Kenton
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
I'm glad Kenton has solved your problem.
I'll add some clarifications.
The & in the first replacement represents the text matching the entire search pattern, that is, nothing at all ($ is an anchor: it matches a position, not text). So it can be omitted:
I'll add some clarifications.
The & in the first replacement represents the text matching the entire search pattern, that is, nothing at all ($ is an anchor: it matches a position, not text). So it can be omitted:
If there is no % in the text then [^%] is the same as . (a full stop or period). So the second search and replace expressions can be simplified:Find what: $
Replace with: [lots of spaces]
You never need to match "the rest of the line" (unless you want to replace it with something). So the second search and replace expressions can be simplified:Find what: ^(.{100}).*$
Replace with: \1
But if you want to guarantee that you never chop anything that isn't trailing space, you might do something like this (note the space before the *):Find what: ^.{100}
Replace with: \0
If you don't use Posix syntax you have to escape parentheses ( ( ) ) as well as braces ( { } ).Find what: ^(.{100}) *$
Replace with: \1
no it can not be simplified.ben_josephs wrote: You never need to match "the rest of the line" (unless you want to replace it with something). So the second search and replace expressions can be simplified:
Purpose of the second search and replace is to replace everything after the first 100 characters by nothing.
Replacing ^.{100} by \0 is a very complicated way of doing nothing at all - find something and replace it by the exact same something ...
The trick with ^(.{100}).*$ is, that it matches the hole line, and by replacing it with \1 - i.e. with the first 100 characters - everything after the first 100 characters will be removed.
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm