右对齐文件

发布于 2024-10-02 08:21:13 字数 37 浏览 5 评论 0原文

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

I need function for right-aligning my file. Could you give me some hint or suggestion?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事↓在人 2024-10-09 08:21:13
while read line
do
  printf '%80s\n' "$line"
done < infile.txt > outfile.txt
while read line
do
  printf '%80s\n' "$line"
done < infile.txt > outfile.txt
雨的味道风的声音 2024-10-09 08:21:13

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

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

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]
                                      }
                                      }
灯角 2024-10-09 08:21:13

编辑:

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

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

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
筱果果 2024-10-09 08:21:13

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文