author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 00:43:11 +0100 | |
branch | v_0 |
changeset 329 | 5bc2bb8b7946 |
parent 256 | 822ffd23d679 |
permissions | -rw-r--r-- |
255
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
<stránka |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
<nadpis>Integrating Relational pipes with GNU Recutils</nadpis> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
<perex>using recfile format as input and output + filtering</perex> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
<m:pořadí-příkladu>01900</m:pořadí-příkladu> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml"> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
Recfile is the native format of <a href="https://www.gnu.org/software/recutils/">GNU Recutils</a>. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
Recfiles are text files that contain records of various types. |
256 | 14 |
They are human-editable and serve as simple databases. |
255
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
<m:name/> support input and output in this format since v0.11. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
We can convert any relational data to the recfile format by using <code>relpipe-out-recfile</code> – e.g. our <code>fstab</code> will look like this: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
<m:pre jazyk="text" src="examples/relpipe-out-fstab.rec.txt"/> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
Then we can edit this data (e.g. in GNU Emacs which has mode for this format) or store it in a version control system like Mercurial or Git. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
Because it is a text format (like XML, which is also supported and good for this purpose), |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
we can efficiently track changes in data across versions, do <code>diff</code> or (with some care) even <code>patch</code>. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
And we can use whole GNU Recutils toolchain while working with such data. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
Obligatory example of filtering our <code>fstab</code>: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab | relpipe-out-recfile | recsel -e "type = 'btrfs' || type = 'xfs'"]]></m:pre> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
<p>Will give us a recfile:</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
<m:pre jazyk="text"><![CDATA[scheme: UUID |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
device: a2b5f230-a795-4f6f-a39b-9b57686c86d5 |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
mount_point: /home |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
type: btrfs |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
options: relatime |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
dump: 0 |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
pass: 2 |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
scheme: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
device: /dev/mapper/sdf_crypt |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
mount_point: /mnt/private |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
type: xfs |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
options: relatime |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
dump: 0 |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
pass: 2]]></m:pre> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
<p>And we can convert it back to the relational format using <code>relpipe-in-recfile</code>:</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
| relpipe-out-recfile \ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
| recsel -e "type = 'btrfs' || type = 'xfs'" \ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
| relpipe-in-recfile \ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
| relpipe-out-tabular]]></m:pre> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
<p>and print as a table in our terminal:</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
<m:pre jazyk="text"><![CDATA[recfile: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬──────────────────┬───────────────┬───────────────╮ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
│ scheme (string) │ device (string) │ mount_point (string) │ type (string) │ options (string) │ dump (string) │ pass (string) │ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼──────────────────┼───────────────┼───────────────┤ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
│ UUID │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home │ btrfs │ relatime │ 0 │ 2 │ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
│ │ /dev/mapper/sdf_crypt │ /mnt/private │ xfs │ relatime │ 0 │ 2 │ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴──────────────────┴───────────────┴───────────────╯ |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
Record count: 2]]></m:pre> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
n.b. in the v0.11 the conversion to recfiles and back is not 100% lossless (unlike XML) |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
because <m:name/> support only three data types (string, unsigned integer and boolean) in this version; |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
this will be improved in later releases (more data types are planned before v1.0) |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
<p> |
256 | 80 |
Because some web browsers or tools can store the original URL in extended attributes while downloading a file, |
81 |
we can use <code>recsel</code> to find files downloaded from some particular domain: |
|
82 |
</p> |
|
83 |
<m:pre jazyk="bash"><![CDATA[find -print0 | relpipe-in-filesystem \ |
|
84 |
--file path \ |
|
85 |
--file size \ |
|
86 |
--file type \ |
|
87 |
--xattr xdg.origin.url --as url \ |
|
88 |
| relpipe-out-recfile \ |
|
89 |
| recsel -e 'url ~ "^https?://([^/]*\.)?archive\.org/"']]></m:pre> |
|
90 |
||
91 |
<p> |
|
255
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
<m:name/> can be also used together with <a href="https://sql-dk.globalcode.info/">SQL-DK</a> (in 2019-03-05 development version) |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
to pipe data from big relational databases like PostgreSQL or MariaDB to other formats like recfiles. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
Having a script: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
256 | 97 |
<m:pre jazyk="bash" src="examples/sql-dk_pg_1.sh" odkaz="ano"/> |
255
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
We can convert result sets from any SQL queries to relational format and then work with such data without connection to the original database. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
Thus we can cache (<em>materialize</em>) the results locally in a file and use them even offline. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
Or we can run the SQL query each time and have fresh data: |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
<m:pre jazyk="text"><![CDATA[sql-dk_pg_1.sh | relpipe-out-recfile]]></m:pre> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
<p>Will result in:</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
<m:pre jazyk="text" src="examples/sql-dk_pg_1.rec.txt"/> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
<p>Or we can view the data in classic tabular way using <code>relpipe-out-tabular</code>:</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
<m:pre jazyk="text" src="examples/sql-dk_pg_1.tabular.txt"/> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
Materialized (or fresh) data from the database can be further transformed |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
using <code>relpipe-tr-*</code> commands like grep, sed, cut, guile, |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
or (through the recfile conversion) by the <code>recsel</code> command from GNU Recutils. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
<p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
The <code>relpipe-in-recfile</code> will help with conversion of recfiles to various formats like XHTML, |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
pretty-printing or with xargs-like processing |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
(using <code>relpipe-out-nullbyte</code> and regular <code>xargs</code> or <code>read_nullbyte</code> function |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
as described in the <m:a href="examples-out-bash">Writing an output filter in Bash</m:a> example). |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
Thus we can have data-driven Bash scripts based on our recfiles. |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
</p> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
</text> |
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
129 |
|
94b533007e77
examples: recfile (GNU Recutils) input and output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
130 |
</stránka> |