BASH输出列格式 [英] BASH output column formatting

查看:429
本文介绍了BASH输出列格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次发帖。你好,世界。工作只是简单地检查,如果我的网站的列表在线,然后返回HTTP code和花费的时间要返回到另一个文件我桌面上的我的额第一个脚本。

First time posting. HELLO WORLD. Working on my first script that just simply checks if a list of my websites are online and then returns the HTTP code and the amount of time it took to return that to another file on my desktop.

- 此命令脚本将运行在Mac OSX上 -

-- THIS SCRIPT WILL BE RUNNING ON MAC OSX --

我想修改我的脚本,以便它格式化输出到3列整齐

I would like to amend my script so that it formats its output into 3 neat columns.

目前

#!/bin/bash
file="/Users/USER12/Desktop/url-list.txt"
printf "" > /Users/USER12/Desktop/url-results.txt
while read line
do
    printf "$line" >> /Users/USER12/Desktop/url-results.txt
    printf "\t\t\t\t" >> /Users/USER12/Desktop/url-results.txt
    curl -o /dev/null --silent --head --write-out '%{http_code} %{time_total}' "$line" >> /Users/USER12/Desktop/url-results.txt
    printf "\n" >> /Users/USER12/Desktop/url-results.txt
done <"$file"

,其输出以下面的格式

which outputs in the following format

google.com              200 0.389
facebook.com                200 0.511
abnormallyLongDomain.com                200 0.786

但我想格式化成整齐的列对齐,方便阅读

but i would like to format into neat aligned columns for easy reading

DOMAIN_NAME                 HTTP_CODE   RESPONSE_TIME
google.com                  200         0.389
facebook.com                200         0.511
abnormallyLongDomain.com    200         0.486

感谢大家的帮助!

Thanks for the help everyone!!

推荐答案

是非常好的。你是,但是,已经使用的printf ,让你在输出格式精细控制。使用的的printf 功能还允许code至有所简化:

column is very nice. You are, however, already using printf which gives you fine control over the output format. Using printf's features also allows the code to be somewhat simplified:

#!/bin/bash
file="/Users/USER12/Desktop/url-list.txt"
log="/Users/USER12/Desktop/url-results.txt"
fmt="%-25s%-12s%-12s\n"
printf "$fmt" DOMAIN_NAME HTTP_CODE RESPONSE_TIME > "$log"
while read line
do
    read code time < <(curl -o /dev/null --silent --head --write-out '%{http_code} %{time_total}' "$line")
    printf "$fmt" "$line" "$code" "$time" >> "$log"
done <"$file"

通过上面定义的格式,输出如下:

With the above defined format, the output looks like:

DOMAIN_NAME              HTTP_CODE   RESPONSE_TIME
google.com               301         0.305
facebook.com             301         0.415
abnormallyLongDomain.com 000         0.000

您可以微调的输出格式,如间隔或对齐,通过脚本改变 FMT 变量。

You can fine-tune the output format, such as spacing or alignment, by changing the fmt variable in the script.

以上code打开和关闭与每个循环日志文件。这样就可以避免查尔斯达菲指出,只需使用 EXEC 重定向标准输出来之前第一个日志文件的printf 语句:

The above code opens and closes the log file with each loop. This can be avoided as Charles Duffy suggests, simply by using exec to redirect stdout to the log file before the first printf statement:

#!/bin/bash
file="/Users/USER12/Desktop/url-list.txt"
exec >"/Users/USER12/Desktop/url-results.txt"
fmt="%-25s%-12s%-12s\n"
printf "$fmt" DOMAIN_NAME HTTP_CODE RESPONSE_TIME
while read line
do
    read code time < <(curl -o /dev/null --silent --head --write-out '%{http_code} %{time_total}' "$line")
    printf "$fmt" "$line" "$code" "$time"
done <"$file"

另外,作为Chepner显示,打印报表可以归纳:​​

Alternatively, as Chepner suggests, the print statements can be grouped:

#!/bin/bash
file="/Users/USER12/Desktop/url-list.txt"
fmt="%-25s%-12s%-12s\n"
{
printf "$fmt" DOMAIN_NAME HTTP_CODE RESPONSE_TIME
while read line
do
    read code time < <(curl -o /dev/null --silent --head --write-out '%{http_code} %{time_total}' "$line")
    printf "$fmt" "$line" "$code" "$time"
done <"$file"
} >"/Users/USER12/Desktop/url-results.txt"

分组的一个优点是,该组后,由于输出被自动恢复到正常的值。

An advantage of grouping is that, after the group, stdout is automatically restored to its normal value.

这篇关于BASH输出列格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆