Created
October 17, 2018 13:05
-
-
Save bAndie91/382837b8b8779ded8ae29a5229fd8cf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# format tab-delimited lines to table with borders | |
# compute width of each column | |
column_width=() | |
text='' | |
while read -r line | |
do | |
text=$text${text:+$'\n'}$line | |
IFS=$'\t' | |
cells=($line) | |
IFS=$'\n\t ' | |
column=0 | |
for cell in "${cells[@]}" | |
do | |
width=${#cell} | |
if [ -z "${column_width[$column]}" ] || [ $width -gt "${column_width[$column]}" ] | |
then | |
column_width[$column]=$width | |
fi | |
let column++ | |
done | |
done | |
# compose the format string | |
fmt='' | |
segments=() | |
for column in "${!column_width[@]}" | |
do | |
fmt="$fmt|%${column_width[$column]}s" | |
segments+=('') | |
done | |
fmt="$fmt|" | |
vborder=`printf -- "$fmt\n" "${segments[@]}" | tr "| " "+-"` | |
# print the table | |
while read -r line | |
do | |
IFS=$'\t' | |
cells=($line) | |
IFS=$'\n\t ' | |
echo "$vborder" | |
printf -- "$fmt\n" "${cells[@]}" | |
done <<<"$text" | |
echo "$vborder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment