右对齐文件

时间:2010-11-12 20:49:07

标签: bash shell alignment right-align

我需要正确对齐文件的功能。你能给我一些暗示或建议吗? 感谢。

4 个答案:

答案 0 :(得分:3)

while read line
do
  printf '%80s\n' "$line"
done < infile.txt > outfile.txt

答案 1 :(得分:2)

我只想到一种方法来回答这个问题:

 % ./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]
                                      }
                                      }

答案 2 :(得分:1)

修改

实际上,您不需要反转这些行:

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

答案 3 :(得分:0)

您需要检测文件中一行的最大长度,并编写一个函数来填充带有此长度空格的行。