在 awk printf 中等于 space

Have equal space in awk printf

我有一个包含以下内容的 csv 文件。

"TELUS","Vancouver Canada","67.151","0.325","0","63247942","64210661","768241424","858087937"
"TELUS","Vancouver Canada","7.151","0.325","0","63247942","64210661","768241424","858087937"

在 awk 的帮助下,我已经成功地将它们解析为可读格式。

awk -F, '{gsub(/"/, ""); printf "| %-11s | %-17s | %.1f %-6s | %.1f %-6s | %.1f %-6s | %.1f %-5.16s |\n", , , /125000, "MB/s", /125000, "MB/s", (+)/(1000*1000*1000), "GB", , "ms" }'

Return这个

| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 67.2 ms    |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 7.2 ms    |

但是如您所见,space 在延迟字段上不相等。我希望它是这样的。

| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 67.2 ms   |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 7.2 ms    |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 947.2 ms  |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 7241.2 ms |

我在上面放了两个额外的字段,重点是无论延迟有多大,我都想保持列间距不变。如何做到这一点?

尝试{%9.3f %-3.16s}:

echo "${a}" | mawk  '{ gsub(/[]/,"")

    printf "| %-11s | %-17s | %.1f %-6s | %.1f %-6s  "\
           "| %.1f %-6s | %9.3f %-3.16s |\n",\
                        ,     ,
                 * 8E-6 , "MB/s",\
                 * 8E-6 , "MB/s", \
            (+)*(1E-9), "GB"  , , "ms" }' FS=','

| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s    | 1.6 GB     |    67.151 ms  |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s    | 1.6 GB     |     7.151 ms  |

您可以使用这个 awk 解决方案:

cat fmt.awk
{
   gsub(/"/, "")
   printf ("| %-11s | %-17s | %.1f %-6s | %.1f %-6s | %.1f %-6s | %-10s |\n",
      , , /125000, "MB/s", /125000, "MB/s", (+)/(1000*1000*1000),
      "GB", sprintf("%.1f ms", ))
}

awk -F, -f fmt.awk file.csv

| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 67.2 ms    |
| TELUS       | Vancouver Canada  | 513.7 MB/s   | 506.0 MB/s   | 1.6 GB     | 7.2 ms     |

使用 sprintf("%.1f ms", )</code> 后跟 <code> ms 得到个位数的小数点。这是在 printf 中格式化为 %-10s 格式以获得左对齐输出 width=10.

要使最后一列右对齐,请使用 %10s 格式。

而不是 hard-coding 字段宽度,只需使用 awk 计算值并使用 column 计算宽度:

$ awk 'BEGIN{FS=OFS=","} {gsub(/"/, ""); printf ",%s,%s,%.1f MB/s,%.1f MB/s,%.1f GB,%.1f ms, \n", , , /125000, /125000, (+)/(1000*1000*1000), }' file |
    column -t -s',' -o' | ' | sed 's/^ //; s/ $//'
| TELUS | Vancouver Canada | 513.7 MB/s | 506.0 MB/s | 1.6 GB | 67.2 ms |
| TELUS | Vancouver Canada | 513.7 MB/s | 506.0 MB/s | 1.6 GB | 7.2 ms  |

但如果您想在 awk 中进行格式化,则再次将字段宽度(例如 %-17s)与字段内容(例如四舍五入为 %.1f)分开:

$ awk -F, -v CONVFMT='%.1f' '{gsub(/"/, ""); printf "| %-11s | %-17s | %-10s | %-10s | %-10s | %-10s |\n", , , /125000 " MB/s", /125000 " MB/s", (+)/(1000*1000*1000) " GB",  " ms" }' file
| TELUS       | Vancouver Canada  | 513.7 MB/s | 506.0 MB/s | 1.6 GB     | 67.151 ms  |
| TELUS       | Vancouver Canada  | 513.7 MB/s | 506.0 MB/s | 1.6 GB     | 7.151 ms   |