庇床铍 发表于 2025-6-8 12:54:30

一个能够生成 Markdown 表格的 Bash 脚本

哈喽大家好,我是咸鱼。
今天分享一个很实用的 bash 脚本,可以通过手动提供单元格内容和列数或者将带有分隔符的文件(如 CSV、TSV 文件)转换为 Markdown 表格。
源代码在文末哦!原文链接:https://josh.fail/2022/pure-bash-markdown-table-generator/
具体功能:

[*]手动生成表格:允许用户输入表格内容和列数,生成标准的 Markdown 格式表格。
[*]文件转换:可以将 CSV 或 TSV 文件转换为 Markdown 表格。
[*]选项支持:支持指定列数、设置分隔符、解析 CSV/TSV 文件等选项。
主要选项:

[*]-COLUMNS:设置生成表格的列数。
[*]-sSEPARATOR:自定义输入文件的列分隔符。
[*]--csv 和 --tsv:分别用于解析 CSV 和 TSV 文件。
几个月前,我想要一个便携式的 Markdown 表格生成器,于是写了这个 markdown-table 脚本。
一开始我只是想传入一堆参数和列数,并让它生成相应的 Markdown 表格。就像下面的例子:
markdown-table -4 \
"Heading 1""Heading 2" "Heading 3" "Heading 4" \
"Hi"         "There"   "From"      "Markdown\!" \
"Everything" "Is"      "So"      "Nicely Aligned\!"当我实现了这一功能后,我意识到还可以添加支持解析带有自定义分隔符的文件,比如 CSV 或 TSV。
markdown-table --tsv < test.tsv上面两种方法都会生成一个 Markdown 表格:
| Heading 1| Heading 2 | Heading 3 | Heading 4      |
| ---------- | --------- | --------- | -------------- |
| Hi         | There   | From      | Markdown       |
| Everything | Is      | So      | Nicely Aligned |#!/usr/bin/env bash# Usage: markdown-table -COLUMNS #      markdown-table -sSEPARATOR < file## NAME#   markdown-table -- generate markdown tables## SYNOPSIS#   markdown-table -COLUMNS #   markdown-table -sSEPARATOR < file## DESCRIPTION#   markdown-table helps generate markdown tables. Manually supply arguments#   and a column count to generate a table, or pass in a delimited file to#   convert to a table.## OPTIONS#   -COLUMNS#       Number of columns to include in output.##   -sSEPARATOR#       String used to separate columns in input files.##   --csv#       Shortcut for `-s,` to parse CSV files. Note that this is a "dumb" CSV#       parser -- it won't work if your cells contain commas!##   --tsv#       Shortcut for `-s$'\t'` to parse TSV files.##   -h, --help#       Prints help text and exits.## EXAMPLES#   Build a 4 column markdown table from arguments:#   markdown-table -4 \#       "Heading 1""Heading 2" "Heading 3" "Heading 4" \#       "Hi"         "There"   "From"      "Markdown!" \#       "Everything" "Is"      "So"      "Nicely Aligned!"##   Convert a CSV file into a markdown table:#   markdown-table -s, < some.csv#   markdown-table --csv < some.csv##   Convert a TSV file into a markdown table:#   markdown-table -s$'\t' < test.tsv#   markdown-table --tsv < test.tsv# Call this script with DEBUG=1 to add some debugging outputif [[ "$DEBUG" ]]; thenexport PS4='+ [${BASH_SOURCE##*/}:${LINENO}] 'set -xfiset -e# Echoes given args to STDERR## $@ - args to pass to echowarn() {echo "$@" >&2}# Print the help text for this program## $1 - flag used to ask for help ("-h" or "--help")print_help() {sed -ne '/^#/!q;s/^#$/# /;/^# /s/^# //p' < "$0" |    awk -v f="$1" '      f == "-h" && ($1 == "Usage:" || u) {      u=1      if ($0 == "") {          exit      } else {          print      }      }      f != "-h"      '}# Returns the highest number in the given arguments## $@ - one or more numeric argumentsmax() {local max=0 argfor arg; do    (( ${arg:-0} > max )) && max="$arg"doneprintf "%s" "$max"}# Formats a table in markdown format## $1 - field separator stringformat_table() {local fs="$1" buffer col current_col=0 current_row=0 min=3local -a lengths=()buffer="$(cat)"# First pass to get column lengthswhile read -r line; do    current_col=0    while read -r col; do      lengths["$current_col"]="$(max "${#col}" "${lengths[$current_col]}")"      current_col=$((current_col + 1))    done

乱蚣 发表于 2025-10-31 04:02:24

分享、互助 让互联网精神温暖你我

裒噎 发表于 2025-12-8 04:23:44

很好很强大我过来先占个楼 待编辑

荦绅诵 发表于 2025-12-25 13:32:18

前排留名,哈哈哈

敖可 发表于 2026-1-12 05:08:11

东西不错很实用谢谢分享

贼瘁 发表于 2026-1-17 06:33:20

过来提前占个楼

揿纰潦 发表于 2026-1-21 09:17:34

感谢分享

湄圳啸 发表于 2026-1-24 07:52:17

这个有用。

撷监芝 发表于 2026-1-26 07:07:38

感谢分享,下载保存了,貌似很强大

浦乐 发表于 2026-1-26 07:42:23

热心回复!

全愉婉 发表于 2026-1-26 11:36:51

yyds。多谢分享

寨亳 发表于 2026-1-26 15:40:32

懂技术并乐意极积无私分享的人越来越少。珍惜

猷咎 发表于 2026-2-3 10:12:29

感谢,下载保存了

颖顿庐 发表于 2026-2-4 06:31:17

这个好,看起来很实用

顶豌 发表于 2026-2-7 22:14:41

感谢,下载保存了

仁夹篇 发表于 2026-2-8 03:39:03

谢谢分享,辛苦了

磁呃泵 发表于 2026-2-8 08:54:56

前排留名,哈哈哈

慎气 发表于 2026-2-9 03:57:34

这个好,看起来很实用

煅圆吧 发表于 2026-2-9 11:29:22

前排留名,哈哈哈

啖曼烟 发表于 2026-2-10 17:38:32

喜欢鼓捣这些软件,现在用得少,谢谢分享!
页: [1] 2
查看完整版本: 一个能够生成 Markdown 表格的 Bash 脚本