Page 1 of 1

emacs org-mode for txt files editing

Posted: Fri Jan 11, 2019 4:47 pm
by Bontrand
Hi,

I've been using org mode in Emacs as an alternative to d2excel for txt files editing, and I thought it wouldn't be a bad idea to share my habits.
Globally org-mode is an Emacs plugin that add logic to plain text files https://orgmode.org/manual/Summary.html#Summary
It has a feature that converts text region in tables, with the command :
M-x org-table-convert-region
Here's an example of the first lines and columns of Weapons.txt :
| name                  | type | type2 | code | alternateGfx | namestr | version | compactsave | rarity | spawnable | mindam | maxdam | 1or2handed | 2handed |
| Hand Axe              | axe  |       | hax  | hax          | hax     |       0 |             |      3 |         1 |      3 |      6 |            |         |
| Axe                   | axe  |       | axe  | axe          | axe     |       0 |             |      4 |         1 |      4 |     11 |            |         |
| Double Axe            | axe  |       | 2ax  | axe          | 2ax     |       0 |             |      4 |         1 |      5 |     13 |            |         |
| Military Pick         | axe  |       | mpi  | axe          | mpi     |       0 |             |      4 |         1 |      7 |     11 |            |         |
| War Axe               | axe  |       | wax  | hax          | wax     |       0 |             |      4 |         1 |     10 |     18 |            |         |
| Large Axe             | axe  |       | lax  | lax          | lax     |       0 |             |      4 |         1 |        |        |            |       1 |
| Broad Axe             | axe  |       | bax  | lax          | bax     |       0 |             |      4 |         1 |        |        |            |       1 |
| Battle Axe            | axe  |       | btx  | btx          | btx     |       0 |             |      4 |         1 |        |        |            |       1 |
| Great Axe             | axe  |       | gax  | btx          | gax     |       0 |             |      4 |         1 |        |        |            |       1 |
| Giant Axe             | axe  |       | gix  | gix          | gix     |       0 |             |      4 |         1 |        |        |            |       1 |
| Wand                  | wand |       | wnd  | wnd          | wnd     |       0 |             |      1 |         1 |      2 |      4 |            |         |
| Yew Wand              | wand |       | ywn  | ywn          | ywn     |       0 |             |      1 |         1 |      2 |      8 |            |         |
| Bone Wand             | wand |       | bwn  | bwn          | bwn     |       0 |             |      1 |         1 |      3 |      7 |            |         |
| Grim Wand             | wand |       | gwn  | bwn          | gwn     |       0 |             |      1 |         1 |      5 |     11 |            |         |
| Club                  | club |       | clb  | clb          | clb     |       0 |             |      1 |         1 |      1 |      6 |            |         |

For me it has proven to be quite efficient when working with txt files.
The only problem is that it had no reverse table command included, so I made my own in bash which uses POSIX sed and this is what I want to share today.
This is by no means a good way of coding, and criticism is of course welcomed, but this had been so useful to me that I still decided to share it.

You call it like this : /path/to/program program txtfile1 txtfile2 ...

Code: Select all

#!/bin/bash                                      


for file in "$@"
do
    read line < "$file"

    if [[ "$line" =~ ^\|.* ]]
    then
        sed -E "s/ +/ /g" -i "$file" #squeeze " "
        sed -E "s/\| ([^|])/|\1/g" -i "$file"
        sed -E "s/([^|]) \|/\1|/g" -i "$file"
        sed -E "s/^\|//g" -i "$file"
        sed -E "s/\|\r//g" -i "$file"
        sed -E "s/\|([^|]+)/\t\1/g" -i "$file"
        sed -E "s/ $//g" -i "$file"
        sed -E 's/ \t/\t/g' -i "$file"
        sed -E 's/$/\r/g' -i "$file"
    fi
done;

Have a good day,

Bontrand