Line Padding

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
matty1stop
Posts: 5
Joined: Mon Jan 09, 2006 4:55 pm

Line Padding

Post by matty1stop »

Anyone know how to pad an entire text file so that each line length is uniform?

Thanks,

Matt
User avatar
qrouton
Posts: 2
Joined: Fri Oct 15, 2004 3:05 pm
Location: Beautiful Central Pennsylvania

Line Padding - options

Post 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
matty1stop
Posts: 5
Joined: Mon Jan 09, 2006 4:55 pm

Post by matty1stop »

Great thanks!
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post 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 ( { } ).
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post 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.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

You're quite right, of course! A severe case of brain failure on my part! Apologies for the confusion.
Post Reply