先上 效果。。。
root@omv:~# time bash draw_table.sh <list.txt
+---------------------------------------------------------------------------------+
| TITLE |
+--------------------+--------------------+--------+--------+-----------+---------+
| #ID | #Name | #Index | #Doing | #Down_Dir | #Status |
+--------------------+--------------------+--------+--------+-----------+---------+
| aidenbrites | Aidenbrites | 35 | replay | null | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| anyway62 | Anyway62 | 132 | replay | offline | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| bruno__revell | Bruno__revell | 750 | replay | offline | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| desnudoindia2 | Desnudoindia2 | 1099 | replay | offline | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| djissues56 | Djissues56 | 1218 | replay | offline | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| |
+---------------------------------------------------------------------------------+
| |
+---------------------------------------------------------------------------------+
| aaaa |
+---------------------------------------------------------------------------------+
| |
+---------------------------------------------------------------------------------+
| sdfadsas |
+--------------------+--------------------+--------+--------+-----------+---------+
| fursaum | Fursaum | 2285 | replay | offline | start |
+--------------------+--------------------+--------+--------+-----------+---------+
| aaa | aa | | | | |
+--------------------+--------------------+--------+--------+-----------+---------+
real 0m0.039s
user 0m0.031s
sys 0m0.009s
root@omv:~# time bash draw_table.sh '%123456789 abcABC' -red,-blue,-green <list.txt
7aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9
A TITLE C
4bbbbbbbbbbbbbbbbbbbb8bbbbbbbbbbbbbbbbbbbb8bbbbbbbb8bbbbbbbb8bbbbbbbbbbb8bbbbbbbbb6
A #ID B #Name B #Index B #Doing B #Down_Dir B #Status C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A aidenbrites B Aidenbrites B 35 B replay B null B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A anyway62 B Anyway62 B 132 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A bearsinmass2 B Bearsinmass2 B 412 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A bgdkmuscleguy B Bgdkmuscleguy B 538 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A bruno__revell B Bruno__revell B 750 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A desnudoindia2 B Desnudoindia2 B 1099 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A djissues56 B Djissues56 B 1218 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A furrymusclemonster B Furrymusclemonster B 2119 B replay B private B start C
4bbbbbbbbbbbbbbbbbbbb2bbbbbbbbbbbbbbbbbbbb2bbbbbbbb2bbbbbbbb2bbbbbbbbbbb2bbbbbbbbb6
A C
4bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb6
A C
4bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb6
A aaaa C
4bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb6
A C
4bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb6
A sdfadsas C
4bbbbbbbbbbbbbbbbbbbb8bbbbbbbbbbbbbbbbbbbb8bbbbbbbb8bbbbbbbb8bbbbbbbbbbb8bbbbbbbbb6
A fursaum B Fursaum B 2285 B replay B offline B start C
4bbbbbbbbbbbbbbbbbbbb5bbbbbbbbbbbbbbbbbbbb5bbbbbbbb5bbbbbbbb5bbbbbbbbbbb5bbbbbbbbb6
A aaa B aa B B B B C
4bbbbbbbbbbbbbbbbbbbb2bbbbbbbbbbbbbbbbbbbb2bbbbbbbb2bbbbbbbb2bbbbbbbbbbb2bbbbbbbbb6
A C
1ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc3
real 0m0.040s
user 0m0.028s
sys 0m0.012s
本来只是想写个在命令行,表格化输出菜单的脚本,写着写着,就写成了通用型的。
脚本的核心,是awk接收输入文本,再格式化输出,为什么不用其它脚本和命令来写呢?
1,shell 在所有linux上通用,不用编译,不用额外安装其它,直接运行,
2,shell 很多命令并不是所有linux都有预装,大量的使用echo,for会降低很多效率,而awk里执行for,变量传递,效率很高。并且只用了echo,case,awk三个命令。大大提高了脚本的通用性。
使用:可以像 column 命令一样的简单使用,draw_table.sh <file.txt 或者 echo -e "A\tB\na\tb"|draw_table.sh
每列以"\t"分隔,每行以"\n"分隔,单列行文本整行居中,默认以shell配色,以"+-|" 为边框,
当然也可以自定义边框,第一个参数是设置边框,像这样 "-1",像这样"%+++++++++,---|||",第一个"%"是参数引导,从第2 位开始,边框以小键盘1~9的九宫格标位,比如第7位是左上角,第9位是右上角,第8位是上边框列分隔,第4位是左边框行分隔。。。特别说明的是,第10位只是文本行空白填充,一般用" ",11-13是行的左中右边填充(分隔)线,14-16是列的上中下边填充(分隔)线。
还可自定义颜色,第二个参数是设置颜色,像这样"-2,-2,-2",像这样"-blue,-blue,-blue",这样"\033[34m,\033[34m,\033[34m",或者"-2,-blue,\033[34m",这是简易模式,以"," 分隔,第1是边框交叉点色,第2是字体色,第3是填充线色。还有完整模式,像这样 "-2,-2,-2,-2........"一共16位,以","分隔,对应边框的位标。
下面是脚本:
#!/bin/bash
#################################################################
# 绘制表格
# 作者:banemon
# 邮箱:banemon@
# Git :https://gitee.com/banemon/linux_sh_script
# 命令:draw_table.sh <file.txt
# 或者:echo -e "A\tB\na\tb"|draw_table.sh
# 帮助:draw_table.sh --help
# 制表符大全
# ─━│┃╌╍╎╏┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╪╫╬═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╔╗╝╚╬═╓╩┠┨┯┷┏┓┗┛┳⊥﹃﹄┌╮╭╯╰╳
# ╲╱
# ╱╲
tbc7="╭";tbc9="╮";
tbc1="╰";tbc3="╯";
tbx0="╳";
# 样式
style="$1"
case $style in
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 2 3 4 5 6 7 8 9 txt_empt top_row mid_row btm_row left_col mid_col right_col
-0) tbs=" ";;
-1) tbs="└┴┘├┼┤┌┬┐ ───│││";;
-2) tbs="└─┘│┼│┌─┐ ───│││";;
-3) tbs="╚╩╝╠╬╣╔╦╗ ═══║║║";;
-4) tbs="╚═╝║╬║╔═╗ ═══║║║";;
-5) tbs="╙╨╜╟╫╢╓╥╖ ───║║║";;
-6) tbs="╘╧╛╞╪╡╒╤╕ ═══│││";;
-7) tbs="└┴┘├┼┤┌┬┐ ─ ─│ │";;
-8) tbs="└─┘│┼│┌─┐ ─ ─│ │";;
-9) tbs="╚╩╝╠╬╣╔╦╗ ═ ═║ ║";;
-10) tbs="╚═╝║╬║╔═╗ ═ ═║ ║";;
-11) tbs="╙╨╜╟╫╢╓╥╖ ─ ─║ ║";;
-12) tbs="╘╧╛╞╪╡╒╤╕ ═ ═│ │";;
-13) tbs="╘╧╛╞╪╡╒╤╕ ═ ═│ │";;
-14) tbs="╚╩╝╠╬╣╔╦╗ ───│││";;
-15) tbs="+++++++++ ---|||";;
"%"*) tbs="${style/"%"/}";;
-h*|--h*)
echo -e '
[ --- HELP --- ]
\t command : draw_table.sh [style] [colors] < <file >
\t pipo : echo -e A\\tB\\na\\tb | draw_table.sh [style] [colors]
\t [style] : input 16 characters
\t 1~9 is Num. keypad as table,10 is not used
\t 11~13 are left,middle,right in a row
\t 14~16 are left,middle,right in a column
\t
\t -0 :
\t -1 :└┴┘├┼┤┌┬┐ ───│││ -9 :╚╩╝╠╬╣╔╦╗ ═ ═║ ║
\t -2 :└─┘│┼│┌─┐ ───│││ -10 :╚═╝║╬║╔═╗ ═ ═║ ║
\t -3 :╚╩╝╠╬╣╔╦╗ ═══║║║ -11 :╙╨╜╟╫╢╓╥╖ ─ ─║ ║
\t -4 :╚═╝║╬║╔═╗ ═══║║║ -12 :╘╧╛╞╪╡╒╤╕ ═ ═│ │
\t -5 :╙╨╜╟╫╢╓╥╖ ───║║║ -13 :╘╧╛╞╪╡╒╤╕ ═ ═│ │
\t -6 :╘╧╛╞╪╡╒╤╕ ═══│││ -14 :╚╩╝╠╬╣╔╦╗ ───│││
\t -7 :└┴┘├┼┤┌┬┐ ─ ─│ │ -15 :+++++++++ ---|||
\t -8 :└─┘│┼│┌─┐ ─ ─│ │
\t
\t [colors]: input a list,like "-3,-4,-8" sames "-green,-yellow,-white"
\t It set color,table cross ,font ,middle. Or \\033[xxm .
\t And support custom color set every characters of sytle
\t Like "\\033[30m,-red,-yellow,,,,,,,,,,,,," sum 16.
\t
\t -1|-black -5|-blue
\t -2|-red -6|-purple
\t -3|-green -7|-cyan
\t -4|-yellow -8|-white
'
exit
;;
esac
tbs="${tbs:-"+++++++++,---|||"}"
# 颜色
color="$2"
case $color in
1) ;;
2) ;;
3) ;;
"-"*|"\033"*)
# 3位数标,词
colors="$color"
;;
"%"*) :
# 全自定义
colors="${color/"%"/}"
;;
esac
colors="${colors:-"-4,-8,-4"}"
# 主体
awk -F '\t' \
-v table_s="$tbs" \
-v color_s="$colors" \
'BEGIN{
}{
for(i=1;i<=NF;i++){
# 每列最大长度
cols_len[i]=cols_len[i]<length($i)?length($i):cols_len[i]
# 每行每列值
rows[NR][i]=$i
}
# 前后行状态
if(NR==1){
befor=0
}else if(1==2){
after=0
}
rows[NR][0] = befor "," NF
befor=NF
}END{
# 颜色表
color_sum = split(color_s,clr_id,",")
if(color_sum==3){
# 简易自定义模式
for(i=1;i<=3;i++){
if(color_s~"-"){
clr_id[i] = color_var(clr_id[i])
}else if(colors~"\033["){
clr_id[i] = cclr_id[i]
}
}
# 组建色表
for(i=1;i<=16;i++){
if(i<10){
colors[i] = clr_id[1]
}else if(i==10){
colors[i] = clr_id[2]
}else if(i>10){
colors[i] = clr_id[3]
}
}
}else if(color_sum==16){
# 全自定义模式
for(i=1;i<=16;i++){
if(color_s~"-"){
clr_id[i] = color_var(clr_id[i])
}else if(colors~"\033["){
clr_id[i] = cclr_id[i]
}
#colors[i] = clr_id[i]
}
}
#split(color_s,colors,",")
clr_end = "\033[0m"
clr_font = colors[10]
#clr_cross = colrs[2]
#clr_blank = colors[3]
# 制表符二维表并着色
for(i=1;i<=length(table_s);i++){
if(colors[i]=="")
tbs[i] = substr(table_s,i,1)
else
tbs[i] = colors[i] substr(table_s,i,1) clr_end
fi
}
# 绘制上边框
top_line=line_val("top")
# 绘制文本行
# 绘制分隔行
mid_line=line_val("mid")
# 绘制下边框
btm_line=line_val("btm")
# 行最大总长度
line_len_sum=0
for(i=1;i<=length(cols_len);i++){
line_len_sum=line_len_sum + cols_len[i] + 2
}
line_len_sum=line_len_sum + length(cols_len) - 1
# 所有表格线预存(提高效率)
title_top = line_val("title_top")
top = line_val("top")
title_mid = line_val("title_mid")
title_btm_mid = line_val("title_btm_mid")
title_top_mid = line_val("title_top_mid")
mid = line_val("mid")
title_btm = line_val("title_btm")
btm = line_val("btm")
# 绘制表格 2
line_rows_sum=length(rows)
for(i=1;i<=line_rows_sum;i++){
# 状态值
split(rows[i][0],status,",")
befors=int(status[1])
nows=int(status[2])
if(i==1 && befors==0){
# 首行时
if(nows<=1){
# 单列
print title_top
print line_val("title_txt",rows[i][1],line_len_sum)
}else if(nows>=2){
# 多列
print top
print line_val("txt",rows[i])
}
}else if(befors<=1){
# 前一行为单列时
if(nows<=1){
# 单列
print title_mid
print line_val("title_txt",rows[i][1],line_len_sum)
}else if(nows>=2){
# 多列
print title_btm_mid
print line_val("txt",rows[i])
}
}else if(befors>=2){
# 前一行为多列时
if(nows<=1){
# 单列
print title_top_mid
print line_val("title_txt",rows[i][1],line_len_sum)
}else if(nows>=2){
# 多列
print mid
print line_val("txt",rows[i])
}
}
# 表格底边
if(i==line_rows_sum && nows<=1){
# 尾行单列时
print title_btm
}else if(i==line_rows_sum && nows>=2){
# 尾行多列时
print btm
}
}
}
function color_var( color){
# 颜色
#local color=$1
#case $color in
if(color=="-1" ||color=="-black"){
n=30
}else if(color=="-2" || color=="-red"){
n=31
}else if(color=="-3" || color=="-green"){
n=32
}else if(color=="-4" || color=="-yellow"){
n=33
}else if(color=="-5" || color=="-blue"){
n=34
}else if(color=="-6" || color=="-purple"){
n=35
}else if(color=="-7" || color=="-cyan"){
n=36
}else if(color=="-8" || color=="-white"){
n=37
}else if(color=="-0" || color=="-reset"){
n=0
}else{
n=0
}
return "\033[" n "m"
}
function line_val( part, txt, cell_lens, cell_len, line, i){
# 更新本次行标
if(part=="top"){
tbs_l=tbs[7]
tbs_m=tbs[8]
tbs_r=tbs[9]
tbs_b=tbs[11]
}else if(part=="mid"){
tbs_l=tbs[4]
tbs_m=tbs[5]
tbs_r=tbs[6]
tbs_b=tbs[12]
}else if(part=="txt"){
tbs_l=tbs[14] tbs[10]
tbs_m=tbs[10] tbs[15] tbs[10]
tbs_r=tbs[10] tbs[16]
tbs_b=tbs[10]
}else if(part=="btm"){
tbs_l=tbs[1]
tbs_m=tbs[2]
tbs_r=tbs[3]
tbs_b=tbs[13]
}else if(part=="title_top"){
tbs_l=tbs[7]
tbs_m=tbs[11]
tbs_r=tbs[9]
tbs_b=tbs[11]
}else if(part=="title_top_mid"){
tbs_l=tbs[4]
tbs_m=tbs[2]
tbs_r=tbs[6]
tbs_b=tbs[12]
}else if(part=="title_mid"){
tbs_l=tbs[4]
tbs_m=tbs[12]
tbs_r=tbs[6]
tbs_b=tbs[12]
}else if(part=="title_txt"){
tbs_l=tbs[14]
tbs_m=tbs[15]
tbs_r=tbs[16]
tbs_b=tbs[10]
}else if(part=="title_btm"){
tbs_l=tbs[1]
tbs_m=tbs[13]
tbs_r=tbs[3]
tbs_b=tbs[13]
}else if(part=="title_btm_mid"){
tbs_l=tbs[4]
tbs_m=tbs[8]
tbs_r=tbs[6]
tbs_b=tbs[12]
}
# 制表符着色
# tbs_l = clr_cross tbs_l clr_end
# tbs_m = clr_cross tbs_m clr_end
# tbs_r = clr_cross tbs_r clr_end
# tbs_b = clr_blank tbs_b clr_end
# title行只有一列文本
if(part=="title_txt"){
cols_count=1
}else{
cols_count=length(cols_len)
}
line_tail=""
for(i=1;i<=cols_count;i++){
# 定义当前单元格内容,长度
if(part=="txt"){
cell_tail=txt[i]
cols_len_new=cols_len[i]-length(cell_tail)
}else if(part=="title_txt"){
# 单列居中
cell_tail=txt
cols_len_new = ( cell_lens - length(cell_tail) ) / 2
cols_len_fix = ( cell_lens - length(cell_tail) ) % 2
#print cols_len_new,cols_len_fix
}else{
cell_tail = ""
cols_len_new = cols_len[i] + 2
}
# 单元格文本着色
cell_tail = clr_font cell_tail clr_end
# 单元格内空白补全
if(part=="title_txt"){
# 单列
#cols_len_new=cols_len_new/2
for(cell_len=1;cell_len<=cols_len_new;cell_len++){
cell_tail= tbs_b cell_tail tbs_b
}
# 单列非偶长度补全
if(cols_len_fix==1){
cell_tail = cell_tail " "
}
}else{
# 多列
for(cell_len=1;cell_len<=cols_len_new;cell_len++){
cell_tail=cell_tail tbs_b
}
}
# 首格
if(i==1){
line_tail=line_tail cell_tail
}else{
# 中格
line_tail=line_tail tbs_m cell_tail
}
# 尾格
if(i==cols_count){
line_tail=line_tail tbs_r
}
}
# 返回行
return tbs_l line_tail
}
'