【发布时间】:2016-06-30 11:09:52
【问题描述】:
我知道有多种方法可以使用 while 或 for 循环以及 awk、echo、printf 等来输出漂亮的列。我正在寻找的是一种简单直观的方法(不凌乱),可以使用 @ 轻松打印列987654321@ 命令和两个 bash 数组。
我找到了以下问答:How can I format the output of a bash command in neat columns
它非常清楚地说明了如何使用column,但该问题询问了 bash 命令并给出了单个命令/字符串作为示例。
我的场景是 - 我有两个 bash 数组(in_db 和 local)。 in_db[$i] 条目是 Y 或 N 如果 local[$i] 条目在我的 mysql 数据库中。我想将in_db 数组的条目作为第一列,将local 数组的条目作为第二列。两列将具有相同数量的条目/行。我不想将任何输出打印到文件中。
我试过了:
column -x -s "**" -t <<< (echo ${in_db[@]}; echo ${local[@]})
in_db 中的条目将在字符串末尾有一个** 以分隔第一列的末尾。我对column 中的-x 标志的假设是它将首先在第一列中打印所有in_db 条目(因此我首先打印in_db)。我知道上面的column 命令打印不符合我的喜好,我显然错过了-x 标志的描述说明。我正在尝试不同的场景和 -flags 以使其正确打印。
我也尝试过在循环中使用来自in_db 的字符串在local 数组之前添加,而不是使用两个单独的数组:
i=0
local_count=${#local[@]}
while [ $i -lt $local_count ]; do
q=$(mysql --defaults-extra-file="/tmp/my.cnf" mp3s \
-e "select folder_location from folders where folder_location = \"${local[$i]}/\"" -s -N)
if [[ -z $q ]]; then
local[$i]="N**${local[$i]}"
else local[$i]="Y**${local[$i]}"
fi
i=$((i+1))
done
echo ${local[@]} | column -s "**" -t
这打印为一行:
N music/atmosphere-sad_clown_bad_dub_13-(rse0095)-bonus_dvd-2008-raceme N music/blink-182 - California (2016) [V0] N music/Vitalogy N music/Pearl Jam - Ten (1991) [V0] N music/Vs N music/Pearl Jam - No Code N music/Pearl Jam - Riot Act N music/Pearl Jam - Transmission Impossible (Live) (2015) [MP3] N music/Pearl Jam N music/Binaural N music/Yield N music/Pearl Jam ~ Backspacer [V0]-2009 Y music/Beastie Boys - The In Sound From Way Out! (V0) N music/Beastie Boys - Licensed To Ill N music/Paul's Boutique 20th Anniversary Remastered Edition [V0] Y music/1992 - Check Your Head Y music/Beastie Boys - Ill Communication N music/Beastie Boys - Hello Nasty N music/Beastie Boys - (2004) - To The 5 Boroughs [V0] N music/Beastie Boys - The Mix-Up (2007) N music/Highway 61 Revisited V0 Y music/1997 - Live Between Us [V0] N music/2006 - World Container [V0] N music/The Tragically Hip - 2012 - Now For Plan A (MP3 V0) Y music/Red Hot Chili Peppers - The Getaway (2016) [V0]
有什么想法吗?
【问题讨论】: