aboutsummaryrefslogtreecommitdiffstats
path: root/TRICKS.md
blob: b14e002198ce0dcd9626488a21706dc06b517ce1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

## Using a text editor

You can enter the ANSI control characters in terminals and many text editors
using "Ctrl-V" escape syntax. For example, to enter the Record Separator
character on a US-style keyboard: while holding the "Ctrl" and "Shift" keys
down, press first "v" and then "6" ("^" is Shift-6). You may be able to get
away with only holding the "Ctrl" modifier and not "Shift". Your editor should
display this "non-printable" character somehow; a common terminal pattern is to
print it as `^^` in a different color.

You may be familiar with this from characters like Carriage Return, which
displays as `^M` and can be entered "Ctrl-V, Ctrl-M".


## Simple conversion with UNIX commands

For simple tables, which can be represented in TSV with with no escaping (eg,
no tab, newline, or ANSI characters in fields), we can transform between AFT
and TSV pretty easily.

From AFT to TSV with `tr` (retaining all header columns):

    cat examples/days.aft | tr -d "\001\002\035" | tr "\036\037" "\t;" | cat -v

From TSV to AFT with `tr` and `sed`:

    echo -n $'\001' > out.aft
    head -n1 examples/days.tsv | tr "\t" "\036" | sed 's/$/\x1D/g' >> out.aft
    echo -n $'\002' >> out.aft
    tail -n +2 examples/days.tsv | tr "\t;" "\036\037" | sed 's/$/\x1D/g' >> out.aft
    cat -v out.aft

It is probably possible to do this with a single `awk` command as well.


## Working with UNIX commands

You can pass input and output delimiter characters to `cut`:

    cut -f1,2 -d $'\036' --output-delimiter $'\t' examples/days.aft

With `awk` you can specify "field separators", for both input and output:

    awk 'BEGIN { FS="\036"; OFS="\t"; RS="\035\n" } {print $1 $3}' examples/days.aft