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

私はこの質問に答える1つの方法しか考えられません:

 % ./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 に答える