8 tabularize - Takes TAB-delimited lines of text and outputs formatted table.
12 I<COMMAND> | tabularize [I<OPTIONS>]
24 borders with nice graphical chars
26 =item -H, --no-horizontal
28 no horizontal lines in the output
30 =item -M, --no-margins
32 no margins, ie. no right-most and left-most vertical borders
34 =item -v, --output-vertical-separator I<CHAR>
36 vertical separator character(s) in the output
38 =item -r, --align-right I<NUM>
40 align these columns (0-indexed) to the right,
41 others are auto-detected and if deemed to contain numeric data,
42 only then aligned to the right.
43 this option is repeatable.
45 =item -l, --align-left I<NUM>
47 similar to --align-right option
57 If B<$PAGER> is set and standard output is a terminal
58 and the resulting table is wider than the terminal,
59 then pipe the table through B<$PAGER>.
65 column(1), untabularize(1)
107 declare -a columnAlignment
=()
128 columnAlignment
[$1]=right
132 columnAlignment
[$1]=left
134 -v|
--output-vertical-separator)
152 echo "$0: unknown option: $1" >&2
165 declare -g -a EXPLODE_ARRAY
=()
167 while [ -n "$string" ]
169 EXPLODE_ARRAY
+=("${string%%$separator*}")
170 new_string
=${string#*$separator}
171 if [ "$string" = "$new_string" ]
178 # return $EXPLODE_ARRAY
181 is_numeric_check_sep
()
183 local thousands_sep
=$2
184 local fraction_sep
=$3
185 [[ $1 =~ ^
[+-]?
[0-9]+($thousands_sep[0-9]+)*($fraction_sep[0-9]+($thousands_sep[0-9]+)*)?$
]]
189 is_numeric_check_sep
"$1" ',' .
&& return 0
190 is_numeric_check_sep
"$1" ' ' , && return 0
191 is_numeric_check_sep
"$1" '.' , && return 0
196 # compute width of each column
202 non_numericals_by_col
=()
205 text
="$text${text:+$'\n'}$line"
206 explode $
'\t' "$line"
209 for cell
in "${EXPLODE_ARRAY[@]}"
212 if [ -z "${column_width[$column]}" ] ||
[ $width -gt "${column_width[$column]}" ]
214 column_width
[$column]=$width
217 # guess if this cell has numerical content (skip 1st line as it's likely a header)
218 if [ $num_line -gt 0 -a -n "$cell" -a -z "${columnAlignment[$column]:-}" ]
220 if is_numeric
"$cell"
222 let numericals_by_col
[$column]++
224 let non_numericals_by_col
[$column]++
235 # no input, no output
242 # compose the format string
251 for column in "${!column_width[@]}"
253 if [ -z "${columnAlignment[$column]:-}" ]
255 if [ ${numericals_by_col[$column]:-0} -ge ${non_numericals_by_col[$column]:-0} ]
257 columnAlignment
[$column]=right
259 columnAlignment
[$column]=left
263 table_width
=$
[table_width
+ (column == 0 ?
(0${margins:+1} ? ${#verticalBar} : 0) : ${#verticalBar}) + ${column_width[$column]}]
265 cellFmt
="${cellFmt:-${margins:+$verticalBar}}${sequentColumn:+$verticalBar}%*s"
266 topFmt
="${topFmt:-${margins:+$topleftCorner}}${sequentColumn:+$topCross}%${column_width[$column]}s"
267 innerFmt
="${innerFmt:-${margins:+$leftCross}}${sequentColumn:+$middleCross}%${column_width[$column]}s"
268 bottomFmt
="${bottomFmt:-${margins:+$bottomleftCorner}}${sequentColumn:+$bottomCross}%${column_width[$column]}s"
273 table_width
=$
[table_width
+ (0${margins:+1} ?
${#verticalBar} : 0)]
274 cellFmt
="$cellFmt${margins:+$verticalBar}"
275 topFmt
="$topFmt${margins:+$toprightCorner}"
276 innerFmt
="$innerFmt${margins:+$rightCross}"
277 bottomFmt
="$bottomFmt${margins:+$bottomrightCorner}"
283 if [ $horizontalLines ]
285 topGrid
=`printf -- "$topFmt" "${segments[@]}"`
286 topGrid
=${topGrid// /$horizontalBar}$
'\n'
287 innerGrid
=`printf -- "$innerFmt" "${segments[@]}"`
288 innerGrid
=${innerGrid// /$horizontalBar}$
'\n'
289 bottomGrid
=`printf -- "$bottomFmt" "${segments[@]}"`
290 bottomGrid
=${bottomGrid// /$horizontalBar}$
'\n'
306 explode $
'\t' "$line"
308 for column in "${!column_width[@]}"
310 [ $column -lt ${#EXPLODE_ARRAY[@]} ] && cell
=${EXPLODE_ARRAY[$column]} || cell
=''
312 bytes
=`LANG=C; echo "${#cell}"`
313 swidth
=$
[ ${column_width[$column]} + ( $bytes - $chars ) ]
314 [ ${columnAlignment[$column]} = left
] && swidth
=-$swidth
315 printf_args
+=($swidth "$cell")
317 printf -- "$cellFmt\n" "${printf_args[@]}"
322 echo -n "$bottomGrid"
326 if [ -t 1 -a -n "$PAGER" ]
328 terminal_cols
=`tput cols`
329 if [ "${terminal_cols:-0}" -le $table_width ]
331 print_table_g |
exec "$PAGER"
332 exit ${PIPESTATUS[1]}