Score:4

How do I make a Markdown table of two columns out of a list whose items alternate to each column?

cn flag

I have a long list of common abbreviations for words in periodical titles. In the list, the full word is followed by its abbreviation. For example:

  • Administration
  • Admin.
  • Applied
  • Appl.
  • Administrative
  • Administ.
  • Approximate
  • Approx.

I want to turn the list into a Markdown table, like this:

Word Abbreviation
Administration Admin.
Applied Appl.

The problem is that doing this by hand would take forever. So, I'm looking for some way to do this faster. If it helps, all of the abbreviated forms end with a period (.).

I've looked online but couldn't find anything for this. That's why I'm asking here. Any help?

Score:5
zw flag

Let's assume that we have input file with list from your question. We can fill it using below command:

cat <<EOF > words+abbrs.txt
Administration
Admin.
Applied
Appl.
Administrative
Administ.
Approximate
Approx.
EOF

Creation of Markdown table is possible on Ubuntu using simple scripting as shown below:

  • dumb step-by-step method

    # write table header
    echo "**Word** | **Abbreviation**" > table.md
    echo "- | -" >> table.md
    
    # extract odd lines as words to file words.txt
    awk 'NR%2==1' words+abbrs.txt > words.txt
    # extract even lines as abbreviations to file abbrs.txt
    awk 'NR%2==0' words+abbrs.txt > abbrs.txt
    
    # combine columns from words.txt and abbrs.txt with '|' separator
    paste -d '|' words.txt abbrs.txt >> table.md
    
  • smart one-liner method (thanks to @steeldriver)

    { printf '%s\n' '**Word** | **Abbreviation**' '-|-'; paste -d '|' - - < words+abbrs.txt; } > table.md
    

You will get the Markdown file with the following contents:

$ cat table.md 
**Word** | **Abbreviation**
- | -
Administration|Admin.
Applied|Appl.
Administrative|Administ.
Approximate|Approx.

So it will be rendered to HTML as

Word Abbreviation
Administration Admin.
Applied Appl.
Administrative Administ.
Approximate Approx.

More info about used tools:

hr flag
Couldn't you avoid the awks and intermediate files altogether here? ex. `{ printf '%s\n' '**Word**|**Abbreviation**' '-|-'; paste -d '|' - - < words+abbrs.txt; }`
N0rbert avatar
zw flag
@steeldriver Thanks, added. Could you please recommend me a good AWK and Bash old-school comprehensive online howto or book?
hr flag
I usually find myself referring to [The GNU Awk User’s Guide](https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents) for awk stuff (it's pretty good about highlighting where gawk differs from other implementations). For bash itself I'm not sure really - I like [BashGuide](https://mywiki.wooledge.org/BashGuide) - obviously the man page is comprehensive but I find it difficult to navigate.
N0rbert avatar
zw flag
Thanks, added both pages to bookmarks. https://tldp.org/LDP/abs/html/abs-guide.html also looks good.
Score:3
us flag

If you can use Pandoc, it can convert from CSV to Markdown. Assuming you have a file of words one per line like in N0rbert's answer, you can convert it to a CSV using paste -d, - -, and then send it to Pandoc:

% (printf "%s\n" Word Abbreviation; cat input-file) | paste -d, - - | pandoc -f csv -t markdown_phpextra
| Word           | Abbreviation |
|----------------|--------------|
| Administration | Admin.       |
| Applied        | Appl.        |
| Administrative | Administ.    |
| Approximate    | Approx.      |

Giving:

Word Abbreviation
Administration Admin.
Applied Appl.
Administrative Administ.
Approximate Approx.

(Further formatting of the heading is probably not required since table headers are usually formatted differently.)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.