【问题标题】:How to write output of a bash command in tabular format with a pre-defined spacing如何使用预定义的间距以表格格式编写 bash 命令的输出
【发布时间】:2020-04-13 16:18:19
【问题描述】:
 2   0   0   184
 2   0   0   184
 765311      61423       6454323302  470948
 598500      678403      6454288800  1810469

我使用grep <command output> | cut -d ':' -f 3 | column -x >> $temp 以表格格式写入数据。但是字符串的长度似乎存在巨大差异,导致表格倾斜。我需要以相等的间距格式化这些。我怎样才能做到这一点?

编辑:运行命令的文件包含如下文本:

total_STDIO_OPENS: 176400
total_STDIO_FDOPENS: 0
total_STDIO_READS: 5881999200
total_STDIO_WRITES: 276435
total_STDIO_SEEKS: 0
total_STDIO_FLUSHES: 0
total_STDIO_BYTES_WRITTEN: 7689144
total_STDIO_BYTES_READ: 53954527200
total_STDIO_MAX_BYTE_READ: 917480
total_STDIO_MAX_BYTE_WRITTEN: 176
total_STDIO_FASTEST_RANK: 81759
total_STDIO_FASTEST_RANK_BYTES: 917712
total_STDIO_SLOWEST_RANK: 137230
total_STDIO_SLOWEST_RANK_BYTES: 917729
total_STDIO_F_META_TIME: 235319.135093
total_STDIO_F_WRITE_TIME: 2.271446
total_STDIO_F_READ_TIME: 1204.541221
total_STDIO_F_OPEN_START_TIMESTAMP: 0.000000
total_STDIO_F_CLOSE_START_TIMESTAMP: 0.000000
total_STDIO_F_WRITE_START_TIMESTAMP: 6.714122
total_STDIO_F_READ_START_TIMESTAMP: 0.000000
total_STDIO_F_OPEN_END_TIMESTAMP: 6.782347
total_STDIO_F_CLOSE_END_TIMESTAMP: 6.856372
total_STDIO_F_WRITE_END_TIMESTAMP: 346.306913
total_STDIO_F_READ_END_TIMESTAMP: 6.856351
total_STDIO_F_FASTEST_RANK_TIME: 0.000000
total_STDIO_F_SLOWEST_RANK_TIME: 0.000000
total_STDIO_F_VARIANCE_RANK_TIME: 0.000000
total_STDIO_F_VARIANCE_RANK_BYTES: 0.000000

我是grep'ing 使用grep -Ein 'total_stdio_read|total_posix_write|total_stdio_write|total_posix_read' $file | cut -d ':' -f 3 | column -x >> $temp

期望的输出是:

2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

【问题讨论】:

  • 请在您的问题中添加示例输入(无描述、无图像、无链接)以及该示例输入所需的输出(无评论)。
  • 我得到了输出,但我不知道如何格式化它。
  • 您的示例输入文件仅匹配具有该 grep 模式的两行...产生与所需输出中的数字匹配的结果将很有用。
  • 这是一个相似的文件,只是数字不同。
  • column -t 可能会有所帮助。

标签: bash shell cut text-formatting


【解决方案1】:

我假设您希望每列 10 个字符的长度加上 2 个字符的额外间距。 如果数据不适合列,则增加长度(数据不会被截断)。

xargs 和printf:

$ <your commands> | xargs -n4 printf '%-10s  %-10s  %-10s  %-10s\n'
2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

awk 和printf:

$ <your commands> | awk '{for(i=1;i<=NF;i++) printf "%-10s%s", $i, (i==NF ? ORS : "  ")}'
2           0           0           184
2           0           0           184
765311      61423       6454323302  470948
598500      678403      6454288800  1810469

或者干脆column -t:

$ <your commands> | column -t
2       0       0           184
2       0       0           184
765311  61423   6454323302  470948
598500  678403  6454288800  1810469

【讨论】:

  • %-10s 由于某种原因没有向终端打印任何内容,所以我尝试用 %-10d 替换它,但这导致 expected a numeric value 错误
  • 每个%... 格式选项都需要一个参数,例如在第一个示例中,每行有四个参数 (-n4) 和四个 %-10s 格式选项,在第二个示例中,有两个(实际值和间距/换行符)。
  • 我的意思是 %-10s %-10s %-10s %-10s %-10s
【解决方案2】:

使用 BASH 'printf' 内置。这与 C printf() 具有相同的格式化语法,因此有很多选项。

例子:

$ printf "%-20s %3f\n" abc 3.4
abc                  3.400000

【讨论】:

    【解决方案3】:

    所以对我有用的是混合解决方案。

    &lt;command output&gt; | cut -d ' ' -f 2 | column -x | xargs -n4 printf '%-10d %-10d %-10d %-10d\n'

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 2012-02-05
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多