shell下格式化输出为表格样式

#!/bin/bash
#Filename:      format_table.sh
#Revision:      0.2
#Date:          2017/8/23
#Author:        sunlinyao
#Description:   shell下格式化输出为表格样式
#               使用时首先需要调用set_title对表格初始化
#               追加表格数据可使用append_cell和append_line,append_cell不会自动换行,换行必须要使用append_line
#               append_line参数是可选的,并且会自动对之前的append_cell换行
#               使用output_table可输出表格
#               暂不支持修改/插入/删除数据
#               可使用. format_table.sh 或者source format_table.sh来引入改脚本的函数
#               "(*)"会自动着色为红色字体
#注:以Centos6.5标准写的,在其他系统上可能结果有差异,欢迎大家测试使用以及反馈八阿哥
# [root@virnet ~]# bash format_table.sh 
# +----+------+---------------+
# |ID  |Name  |Creation time  |
# +----+------+---------------+
# |1   |TF    |2017-01-01     |
# |2   |      |2017-01-02(*)  |
# |3   |SF    |               |
# |3   |SF    |(*)            |
# |4   |TS    |               |
# |5   |      |               |
# +----+------+---------------+


sep="#"
function append_cell(){
    #对表格追加单元格
    #append_cell col0 "col 1" ""
    #append_cell col3
    local i
    for i in "$@"
    do
        line+="|$i${sep}"
    done
}
function check_line(){
if [ -n "$line" ] 
then
    c_c=$(echo $line|tr -cd "${sep}"|wc -c)
    difference=$((${column_count}-${c_c}))
    if [ $difference -gt 0 ]
    then
        line+=$(seq -s " " $difference|sed -r s/[0-9]\+/\|${sep}/g|sed -r  s/${sep}\ /${sep}/g)
    fi
    content+="${line}|\n"
fi

}
function append_line(){
    check_line
    line=""
    local i
    for i in "$@"
    do
        line+="|$i${sep}"
    done
    check_line
    line=""
}
function segmentation(){
    local seg=""
    local i
    for i in $(seq $column_count)
    do 
        seg+="+${sep}"
    done
    seg+="${sep}+\n"
    echo $seg
}
function set_title(){
    #表格标头,以空格分割,包含空格的字符串用引号,如
    #set_title Column_0 "Column 1" "" Column3
    [ -n "$title" ] && echo "Warring:表头已经定义过,重写表头和内容"
    column_count=0
    title=""
    local i
    for i in "$@"
    do
        title+="|${i}${sep}"
        let column_count++
    done
    title+="|\n"
    seg=`segmentation`
    title="${seg}${title}${seg}"
    content=""
}
function output_table(){
    if [ ! -n "${title}" ] 
    then
        echo "未设置表头,退出" && return 1
    fi
    append_line
    table="${title}${content}$(segmentation)"
    echo -e $table|column -s "${sep}" -t|awk '{if($0 ~ /^+/){gsub(" ","-",$0);print $0}else{gsub("\\(\\*\\)","\033[31m(*)\033[0m",$0);print $0}}'

}

if [ "$SHLVL" -eq "2" ]
then
    set_title ID Name "Creation time"
    append_line 1 "TF" "2017-01-01"
    append_cell 2 "" "2017-01-02(*)"
    append_line 
    append_cell 3 "SF"
    append_line 
    append_cell 3 "SF" "(*)"
    append_line 4 "TS"
    append_cell 5
    output_table
fi
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值