head -n 2 table.md && tail -n +3 table.md | sort --field-separator=\| --key=1) > sorted-table.md
Explanation:
head -n 2 table.md
- this will take the first two lines of table.md which will be the column titles
tail -n +3 table.md
- this will read from the bottom of the file up to (and including) the 3rd line (should be the first line of table data)
sort --field-separator=\| --key=1
- this will sort based on the first field. The fields are delimited by the pipe (|
) character which needs to be escaped using the backslash (\
) as it is a special character
> sorted-table.md
- this will dump the output of the commands in the brackets in to a file called "sorted-table.md"
Enclosing the head
, tail
, and sort
commands in parentheses ensures that the all the output is captured and dumped in to the target file. If the parentheses are missed, then the table headers will echo in to the terminal and only the table data will be dumped in to the file.
Markdown tables can be left, right or centre aligned. See the example code below, note the position of the colons (:
).
| Col 1 | Col 2 | Col 3 |
|:--|:--:|--:|
| This is left | This is centre | This is right |
Col 1 | Col 2 | Col 3 |
---|---|---|
This is left | This is centre | This is right |
Some more text to show the alignment better | This is even more text and should appear centre aligned | And this text will appear with a right alignment |