1

我需要右对齐文件的功能。你能给我一些提示或建议吗?谢谢。

4

4 回答 4

3
while read line
do
  printf '%80s\n' "$line"
done < infile.txt > outfile.txt
于 2010-11-12T20:51:27.183 回答
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]
                                      }
                                      }
于 2010-11-13T08:27:03.010 回答
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
于 2010-11-13T01:01:48.513 回答
0

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

于 2010-11-12T20:51:15.800 回答