Saturday, May 4, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 905  / 1 Year ago, sat, december 24, 2022, 5:24:26

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?


More From » command-line

 Answers
6

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
    ' '**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:



[#912] Sunday, December 25, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
istmasted

Total Points: 287
Total Questions: 130
Total Answers: 153

Location: Burkina Faso
Member since Thu, Dec 23, 2021
2 Years ago
istmasted questions
Sat, Jun 19, 21, 16:54, 3 Years ago
Tue, Mar 1, 22, 22:36, 2 Years ago
Mon, Jan 31, 22, 22:55, 2 Years ago
Sun, Oct 9, 22, 09:33, 2 Years ago
;