258
|
1 |
<stránka
|
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
|
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
|
|
4 |
|
|
5 |
<nadpis>Changing structures with AWK</nadpis>
|
|
6 |
<perex>adding or removing attributes or dropping a relation</perex>
|
|
7 |
<m:pořadí-příkladu>02400</m:pořadí-příkladu>
|
|
8 |
|
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml">
|
|
10 |
|
|
11 |
<p>
|
|
12 |
The AWK transformations can also change the structure of transformed relation.
|
|
13 |
It means adding or removing attributes or dropping the whole relation.
|
|
14 |
</p>
|
|
15 |
|
|
16 |
<h2>Adding attributes with AWK</h2>
|
|
17 |
|
|
18 |
<p>
|
|
19 |
Using <code>--output-attribute</code> we can specify the output attributes.
|
|
20 |
If we do not want to explicitly specify all of them and just want to add some new ones, we will use <code>--input-attributes-append</code> (or <code>--input-attributes-prepend</code>),
|
|
21 |
which will preserve also the input attributes:
|
|
22 |
</p>
|
|
23 |
|
|
24 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
|
|
25 |
| relpipe-tr-awk \
|
|
26 |
--relation '.*' \
|
|
27 |
--for-each '{ id = NR; record(); }' \
|
|
28 |
--output-attribute id integer \
|
|
29 |
--input-attributes-append \
|
|
30 |
| relpipe-out-tabular]]></m:pre>
|
|
31 |
|
|
32 |
<p>This adds one new attribute with ordinal numbers:</p>
|
|
33 |
|
|
34 |
<pre><![CDATA[fstab:
|
|
35 |
╭──────────────┬─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬───────────────────────────────────────┬────────────────┬────────────────╮
|
|
36 |
│ id (integer) │ scheme (string) │ device (string) │ mount_point (string) │ type (string) │ options (string) │ dump (integer) │ pass (integer) │
|
|
37 |
├──────────────┼─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼───────────────────────────────────────┼────────────────┼────────────────┤
|
|
38 |
│ 1 │ UUID │ 29758270-fd25-4a6c-a7bb-9a18302816af │ / │ ext4 │ relatime,user_xattr,errors=remount-ro │ 0 │ 1 │
|
|
39 |
│ 2 │ │ /dev/sr0 │ /media/cdrom0 │ udf,iso9660 │ user,noauto │ 0 │ 0 │
|
|
40 |
│ 3 │ │ /dev/sde │ /mnt/data │ ext4 │ relatime,user_xattr,errors=remount-ro │ 0 │ 2 │
|
|
41 |
│ 4 │ UUID │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home │ btrfs │ relatime │ 0 │ 2 │
|
|
42 |
│ 5 │ │ /dev/mapper/sdf_crypt │ /mnt/private │ xfs │ relatime │ 0 │ 2 │
|
|
43 |
╰──────────────┴─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯
|
|
44 |
Record count: 5]]></pre>
|
|
45 |
|
|
46 |
|
|
47 |
<h2>Remnoving attributes with AWK</h2>
|
|
48 |
|
|
49 |
<p>Or we can omit omit attributes unless explicitly specified ones:</p>
|
|
50 |
|
|
51 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
|
|
52 |
| relpipe-tr-awk \
|
|
53 |
--relation '.*' \
|
|
54 |
--for-each '{ type_big = toupper(type); record(); }' \
|
|
55 |
--output-attribute mount_point string \
|
|
56 |
--output-attribute type string \
|
|
57 |
--output-attribute type_big string \
|
|
58 |
| relpipe-out-tabular]]></m:pre>
|
|
59 |
|
|
60 |
<p>which effectively removes unlisted attributes:</p>
|
|
61 |
|
|
62 |
<pre><![CDATA[fstab:
|
|
63 |
╭──────────────────────┬───────────────┬───────────────────╮
|
|
64 |
│ mount_point (string) │ type (string) │ type_big (string) │
|
|
65 |
├──────────────────────┼───────────────┼───────────────────┤
|
|
66 |
│ / │ ext4 │ EXT4 │
|
|
67 |
│ /media/cdrom0 │ udf,iso9660 │ UDF,ISO9660 │
|
|
68 |
│ /mnt/data │ ext4 │ EXT4 │
|
|
69 |
│ /home │ btrfs │ BTRFS │
|
|
70 |
│ /mnt/private │ xfs │ XFS │
|
|
71 |
╰──────────────────────┴───────────────┴───────────────────╯
|
|
72 |
Record count: 5]]></pre>
|
|
73 |
|
|
74 |
|
|
75 |
<p>AWK is a powerful language so we can use conditions, for cycles etc. and write much more complex transformations.</p>
|
|
76 |
|
|
77 |
<h2>Dropping a relation</h2>
|
|
78 |
|
|
79 |
<p>
|
|
80 |
A relation can be „dropped“ which means that transformation will run but no relational output will be generated for it
|
|
81 |
(even the header will be omitted, so it differs from just eliminating all records by a condition).
|
|
82 |
Using AWK for such a simple operation like <code>DROP</code> seems weird but sometimes it might make sense due to intentional side effects.
|
|
83 |
</p>
|
|
84 |
|
|
85 |
<p>
|
|
86 |
Because the AWK code is executed for each record, we can e.g. write some output to a file or to STDERR:
|
|
87 |
</p>
|
|
88 |
|
|
89 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
|
|
90 |
| relpipe-tr-awk \
|
|
91 |
--relation '.*' \
|
|
92 |
--for-each '{ printf("%s → %s\n", device, mount_point) > "/dev/stderr" }' \
|
|
93 |
--drop]]></m:pre>
|
|
94 |
|
|
95 |
<p>Which prints text:</p>
|
|
96 |
|
|
97 |
<pre><![CDATA[29758270-fd25-4a6c-a7bb-9a18302816af → /
|
|
98 |
/dev/sr0 → /media/cdrom0
|
|
99 |
/dev/sde → /mnt/data
|
|
100 |
a2b5f230-a795-4f6f-a39b-9b57686c86d5 → /home
|
|
101 |
/dev/mapper/sdf_crypt → /mnt/private]]></pre>
|
|
102 |
|
|
103 |
<p>
|
|
104 |
Then <code>relpipe-tr-awk</code> works much like an output filter (converts relational data to another format).
|
|
105 |
However, if there are more relations and some of theme are not matched by <code>--relation</code>, they will be passed through and delivered to the STDOUT in the relational format.
|
|
106 |
STDERR might be occasionally polluted by some warning messages, so using a dedicated file for such output is a safer way.
|
|
107 |
</p>
|
|
108 |
|
|
109 |
|
|
110 |
</text>
|
|
111 |
|
|
112 |
</stránka>
|