Page 1 of 1

Line Padding

Posted: Mon Jan 09, 2006 4:57 pm
by matty1stop
Anyone know how to pad an entire text file so that each line length is uniform?

Thanks,

Matt

Line Padding - options

Posted: Mon Jan 09, 2006 7:43 pm
by qrouton
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

Posted: Mon Jan 09, 2006 9:10 pm
by matty1stop
Great thanks!

Posted: Tue Jan 10, 2006 10:39 am
by ben_josephs
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:
Find what: $
Replace with: [lots of spaces]
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: ^(.{100}).*$
Replace with: \1
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: \0
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: \1
If you don't use Posix syntax you have to escape parentheses ( ( ) ) as well as braces ( { } ).

Posted: Tue Jan 10, 2006 6:43 pm
by MudGuard
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:
no it can not 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.

Posted: Tue Jan 10, 2006 8:00 pm
by ben_josephs
You're quite right, of course! A severe case of brain failure on my part! Apologies for the confusion.