How can I pad each line of a file to a certain width (say, 63 characters wide), padding with spaces if need be?
For now, let’s assume all lines are guaranteed to be less than 63 characters.
I use Vim and would prefer a way to do it there, where I can select the lines I wish to apply the padding to, and run some sort of a printf %63s current_line
command.
However, I’m certainly open to using sed, awk, or some sort of linux tool to do the job too.
:%s/.*/\=printf('%-63s', submatch(0))
$ awk '{printf "%-63s\n", $0}' testfile > newfile
In Vim, I would use the following command:
:%s/$/\=repeat(' ',64-virtcol('$'))
(The use of the virtcol()
function, as opposed to the col()
one,
is guided by the necessity to properly handle tab characters as well
as multibyte non-ASCII characters that might occur in the text.)
Just for fun, a Perl version:
$ perl -lpe '$_ .= " " x (63 - length $_)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With