右对齐文件

我需要函数来正确对齐我的文件。你能给我一些暗示或建议吗? 谢谢。     
已邀请:
while read line
do
  printf '%80sn' "$line"
done < infile.txt > outfile.txt
    
我只想到一种方法来回答这个问题:
 % ./4168932.awk ./4168932.awk        
                      #!/usr/bin/awk -f

                                      {
                           a[++n] = $0;
            if (length(a[n]) > width) {
                   width = length(a[n])
                                      }
                                      }

                                  END {
              format = "%" width "sn";
    for (line = 1; line <= n; ++line) {
                 printf format, a[line]
                                      }
                                      }
    
编辑: 实际上,您不需要反转这些行:
printf -v spaces "%80s" " "; man rev | sed "s/^/$spaces/;s/.*(.{80})$/1/"
原版的: 反转线条,填充它们,截断它们并将它们反转。
man rev | rev | sed '1{x;s/^$/          /;s/^.*$/&&&&&&&&/;x};G;s/^(.{81}).*$/1/;s/n//' | rev
输出:
  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
这是另一种做同样事情的方法:
printf -v spaces "%80s" " "; man rev | rev | sed "s/$/$spaces/;s/^(.{80}).*$/1/" | rev
    
您需要检测文件中一行的最大长度,并编写一个函数来填充具有此长度空格的行。     

要回复问题请先登录注册