作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 shell 脚本,可以在文件中写入一些数据。我想添加一个标题 "Instance Details"
,它应该在打印文件中的实际数据之前居中对齐。
我试过这段代码:
out="Instances_$today_date"
awk -F'\t' -v of="$out" '
# (input and other code omitted...)
BEGIN { # Set the printf() format string for the header and the data lines.
fmt = "%-12s %-33s %-15s %s\n"
# Print the header
printf("Instance Details") > of
printf(fmt, "Instance id", "Name", "Owner", "Cost.centre") > of
}'
但是我在执行这段代码后得到的输出是:
Instance DetailsInstance id Name Owner Cost.centre
预期输出:
Instance Details
Instance id Name Owner Cost.centre
非常感谢任何线索。
最佳答案
尝试以下操作:
awk -F'\t' -v of="$out" '
# (input and other code omitted...)
BEGIN { # Set the printf() format string for the header and the data lines.
# Print the header
headerText="Instance Details"
headerMaxLen=74
padding=(length(headerText) - headerMaxLen) / 2
printf("%" padding "s" "%s" "%" padding "s" "\n\n\n", "", headerText, "") > of
fmt="%-12s %-33s %-15s %s\n"
printf(fmt, "Instance id", "Name", "Owner", "Cost.centre") >> of
}'
of
传递输出文件的名称。Instance Details
两边的必要填充,使其显示在输出行的中心(行长度源自最终输出行的长度)。Instance Details
,其中包含 3 个换行符以创建所需的空行。printf
语句使用 > of
还是 >> of
并不重要> - 在任一情况下,文件都被附加到。)关于linux - 如何在shell脚本生成的文件顶部写标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800766/
我是一名优秀的程序员,十分优秀!