I need function for right-aligning my file. Could you give me some hint or suggestion? Thanks.
Asked
Active
Viewed 413 times
1
-
4Please follow [general](http://tinyurl.com/so-hints) question [guidelines](http://meta.stackexchange.com/q/10812): state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. – Nov 12 '10 at 20:55
4 Answers
3
while read line
do
printf '%80s\n' "$line"
done < infile.txt > outfile.txt

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
2
I can only think of one way to answer this question:
% ./4168932.awk ./4168932.awk
#!/usr/bin/awk -f
{
a[++n] = $0;
if (length(a[n]) > width) {
width = length(a[n])
}
}
END {
format = "%" width "s\n";
for (line = 1; line <= n; ++line) {
printf format, a[line]
}
}

johnsyweb
- 136,902
- 23
- 188
- 247
1
Edit:
Actually, you don't need to reverse the lines:
printf -v spaces "%80s" " "; man rev | sed "s/^/$spaces/;s/.*\(.\{80\}\)\$/\1/"
Original:
Reverse the lines, pad them, truncate them and reverse them back.
man rev | rev | sed '1{x;s/^$/ /;s/^.*$/&&&&&&&&/;x};G;s/^\(.\{81\}\).*$/\1/;s/\n//' | rev
Output:
REV(1) BSD General Commands Manual REV(1)
NAME
rev — reverse lines of a file or files
SYNOPSIS
rev [file ...]
DESCRIPTION
The rev utility copies the specified files to the standard output,
reversing the order of characters in every line. If no files are speci‐
fied, the standard input is read.
AVAILABILITY
The rev command is part of the util-linux-ng package and is available
from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
BSD March 21, 1992 BSD
Here's another way to do the same thing:
printf -v spaces "%80s" " "; man rev | rev | sed "s/\$/$spaces/;s/^\(.\{80\}\).*$/\1/" | rev

Dennis Williamson
- 346,391
- 90
- 374
- 439
0
You will need to detect the maximum length of a line in your file, and to write a function to pad lines with spaces to this length.

greg0ire
- 22,714
- 16
- 72
- 101