Skip to content

Instantly share code, notes, and snippets.

@bAndie91
Created October 17, 2018 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bAndie91/382837b8b8779ded8ae29a5229fd8cf4 to your computer and use it in GitHub Desktop.
Save bAndie91/382837b8b8779ded8ae29a5229fd8cf4 to your computer and use it in GitHub Desktop.
# 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