author | František Kučera <franta-hg@frantovo.cz> |
Mon, 21 Feb 2022 00:43:11 +0100 | |
branch | v_0 |
changeset 329 | 5bc2bb8b7946 |
parent 326 | ab7f333f1225 |
permissions | -rw-r--r-- |
23
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
1 |
<stránka |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
2 |
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana" |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
3 |
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"> |
0d2729ed16ed
zkouška interního odkazu
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
4 |
|
244
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
5 |
<nadpis>Doing projection and restriction using cut and grep</nadpis> |
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
6 |
<perex>SELECT mount_point FROM fstab WHERE type IN ('btrfs', 'xfs')</perex> |
d4f401b5f90c
examples: move each example to a separate page + add generated list of examples
František Kučera <franta-hg@frantovo.cz>
parents:
241
diff
changeset
|
7 |
<m:pořadí-příkladu>01000</m:pořadí-příkladu> |
4
1bb39595a51c
genrování hlavní nabídky #1
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
8 |
|
2
ab9099ff88fa
vkládání zápatí, jmenné prostory, saxon
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
9 |
<text xmlns="http://www.w3.org/1999/xhtml"> |
176
46042297e0d6
relpipe-tr-validator: specification and examples
František Kučera <franta-hg@frantovo.cz>
parents:
140
diff
changeset
|
10 |
|
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
11 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
12 |
While reading classic pipelines involving <code>grep</code> and <code>cut</code> commands |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
13 |
we must notice that there is some similarity with simple SQL queries looking like: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
14 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
15 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
16 |
<m:pre jazyk="SQL">SELECT "some", "cut", "fields" FROM stdin WHERE grep_matches(whole_line);</m:pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
17 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
18 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
19 |
And that is true: <code>grep</code> does restriction<m:podČarou> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
20 |
<a href="https://en.wikipedia.org/wiki/Selection_(relational_algebra)">selecting</a> only certain records from the original relation according to their match with given conditions</m:podČarou> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
21 |
and <code>cut</code> does projection<m:podČarou>limited subset of what <a href="https://en.wikipedia.org/wiki/Projection_(relational_algebra)">projection</a> means</m:podČarou>. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
22 |
Now we can do these relational operations using our relational tools called <code>relpipe-tr-grep</code> and <code>relpipe-tr-cut</code>. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
23 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
24 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
25 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
26 |
Assume that we need only <code>mount_point</code> fields from our <code>fstab</code> where <code>type</code> is <code>btrfs</code> or <code>xfs</code> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
27 |
and we want to do something (a shell script block) with these directory paths. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
28 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
29 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
30 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \ |
325
4560c5623d7d
relpipe-tr-grep has new syntax since v0.18: --relation, --attribute, --value
František Kučera <franta-hg@frantovo.cz>
parents:
301
diff
changeset
|
31 |
| relpipe-tr-grep --relation 'fstab' --attribute 'type' --value '^btrfs|xfs$' \ |
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
32 |
| relpipe-tr-cut --relation 'fstab' --attribute 'mount_point' \ |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
33 |
| relpipe-out-nullbyte \ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
34 |
| while read -r -d '' m; do |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
35 |
echo "$m"; |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
36 |
done]]></m:pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
37 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
38 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
39 |
The <code>relpipe-tr-cut</code> tool has similar syntax to its <em>grep</em> and <em>sed</em> siblings and also uses the power of regular expressions. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
40 |
In this case it modifies on-the-fly the <code>fstab</code> relation and drops all its attributes except the <code>mount_point</code> one. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
41 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
42 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
43 |
<p> |
214
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
44 |
Then we pass the data to the Bash <code>while</code> cycle. |
63f31bf2697f
examples: Writing an output filter in Bash
František Kučera <franta-hg@frantovo.cz>
parents:
213
diff
changeset
|
45 |
In such simple scenario (just <code>echo</code>), we could use <code>xargs</code> as in examples above, |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
46 |
but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them. |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
47 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
48 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
49 |
<h2>More projections with relpipe-tr-cut</h2> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
50 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
51 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
52 |
Assume that we have a simple relation containing numbers: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
53 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
54 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
55 |
<m:pre jazyk="bash"><![CDATA[seq 0 8 \ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
56 |
| tr \\n \\0 \ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
57 |
| relpipe-in-cli generate-from-stdin numbers 3 a integer b integer c integer \ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
58 |
> numbers.rp]]></m:pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
59 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
60 |
<p>and second one containing letters:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
61 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
62 |
<m:pre jazyk="bash"><![CDATA[relpipe-in-cli generate letters 2 a string b string A B C D > letters.rp]]></m:pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
63 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
64 |
<p>We saved them into two files and then combined them into a single file. We will work with them as they are a single stream of relations:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
65 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
66 |
<m:pre jazyk="bash"><![CDATA[cat numbers.rp letters.rp > both.rp; |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
67 |
cat both.rp | relpipe-out-tabular]]></m:pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
68 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
69 |
<p>Will print:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
70 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
71 |
<pre><![CDATA[numbers: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
72 |
╭─────────────┬─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
73 |
│ a (integer) │ b (integer) │ c (integer) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
74 |
├─────────────┼─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
75 |
│ 0 │ 1 │ 2 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
76 |
│ 3 │ 4 │ 5 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
77 |
│ 6 │ 7 │ 8 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
78 |
╰─────────────┴─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
79 |
Record count: 3 |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
80 |
letters: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
81 |
╭─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
82 |
│ a (string) │ b (string) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
83 |
├─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
84 |
│ A │ B │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
85 |
│ C │ D │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
86 |
╰─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
87 |
Record count: 2]]></pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
88 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
89 |
<p>We can put away the <code>a</code> attribute from the <code>numbers</code> relation:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
90 |
|
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
91 |
<m:pre jazyk="bash">cat both.rp | relpipe-tr-cut --relation 'numbers' --attribute 'b|c' | relpipe-out-tabular</m:pre> |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
92 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
93 |
<p>and leave the <code>letters</code> relation unaffected:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
94 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
95 |
<pre><![CDATA[numbers: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
96 |
╭─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
97 |
│ b (integer) │ c (integer) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
98 |
├─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
99 |
│ 1 │ 2 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
100 |
│ 4 │ 5 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
101 |
│ 7 │ 8 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
102 |
╰─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
103 |
Record count: 3 |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
104 |
letters: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
105 |
╭─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
106 |
│ a (string) │ b (string) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
107 |
├─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
108 |
│ A │ B │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
109 |
│ C │ D │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
110 |
╰─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
111 |
Record count: 2]]></pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
112 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
113 |
<p>Or we can remove <code>a</code> from both relations resp. keep there only attributes whose names match <code>'b|c'</code> regex:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
114 |
|
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
115 |
<m:pre jazyk="bash">cat both.rp | relpipe-tr-cut --relation '.*' --attribute 'b|c' | relpipe-out-tabular</m:pre> |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
116 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
117 |
<p>Instead of <code>'.*'</code> we could use <code>'numbers|letters'</code> and in this case it will give the same result:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
118 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
119 |
<pre><![CDATA[numbers: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
120 |
╭─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
121 |
│ b (integer) │ c (integer) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
122 |
├─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
123 |
│ 1 │ 2 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
124 |
│ 4 │ 5 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
125 |
│ 7 │ 8 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
126 |
╰─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
127 |
Record count: 3 |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
128 |
letters: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
129 |
╭─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
130 |
│ b (string) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
131 |
├─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
132 |
│ B │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
133 |
│ D │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
134 |
╰─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
135 |
Record count: 2]]></pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
136 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
137 |
<p>All the time, we are reducing the attributes. But we can also multiply them or change their order:</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
138 |
|
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
139 |
<m:pre jazyk="bash">cat both.rp \ |
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
140 |
| relpipe-tr-cut --relation 'numbers' --attribute 'b|a|c' --attribute 'b' --attribute 'a' --attribute 'a' \ |
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
141 |
| relpipe-out-tabular</m:pre> |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
142 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
143 |
<p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
144 |
n.b. the order in <code>'b|a|c'</code> does not matter and if such regex matches, it preserves the original order of the attributes; |
213
cbf25a63a43f
specification: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
212
diff
changeset
|
145 |
but if we use multiple regexes to specify attributes, their order and count matters: |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
146 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
147 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
148 |
<pre><![CDATA[numbers: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
149 |
╭─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
150 |
│ a (integer) │ b (integer) │ c (integer) │ b (integer) │ a (integer) │ a (integer) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
151 |
├─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
152 |
│ 0 │ 1 │ 2 │ 1 │ 0 │ 0 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
153 |
│ 3 │ 4 │ 5 │ 4 │ 3 │ 3 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
154 |
│ 6 │ 7 │ 8 │ 7 │ 6 │ 6 │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
155 |
╰─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
156 |
Record count: 3 |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
157 |
letters: |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
158 |
╭─────────────┬─────────────╮ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
159 |
│ a (string) │ b (string) │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
160 |
├─────────────┼─────────────┤ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
161 |
│ A │ B │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
162 |
│ C │ D │ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
163 |
╰─────────────┴─────────────╯ |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
164 |
Record count: 2]]></pre> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
165 |
|
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
166 |
<p> |
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
167 |
The <code>letters</code> relation stays rock steady and <code>relpipe-tr-cut --relation 'numbers'</code> does not affect it in any way. |
212
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
168 |
</p> |
bf9a704dc916
examples: relpipe-tr-cut
František Kučera <franta-hg@frantovo.cz>
parents:
210
diff
changeset
|
169 |
|
301
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
170 |
<h2>Process CSV files</h2> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
171 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
172 |
<p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
173 |
There are various input filters (<code>relpipe-in-*</code>), one of them is <code>relpipe-in-csv</code> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
174 |
which converts CSV files to relational format. |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
175 |
Thus we can process standard CSV files in our relational pipelines |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
176 |
and e.g. filter records that have certain value in certain column (<code>relpipe-tr-grep</code>) |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
177 |
or keep only certain columns (<code>relpipe-tr-cut</code>). |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
178 |
</p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
179 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
180 |
<p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
181 |
We may have a <code>tasks.csv</code> file containing TODOs and FIXMEs: |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
182 |
</p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
183 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
184 |
<pre><![CDATA["file","line","type","description" |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
185 |
".hg/shelve-backup/posix_mq.patch","97","TODO","support also other encodings." |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
186 |
".hg/shelve-backup/posix_mq.patch","163","TODO","support also other encodings." |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
187 |
"src/FileAttributeFinder.h","79","TODO","optional whitespace trimming or substring" |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
188 |
"src/FileAttributeFinder.h","80","TODO","custom encoding + read encoding from xattr" |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
189 |
"src/FileAttributeFinder.h","83","TODO","allow custom error value or fallback to HEX/Base64" |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
190 |
"streamlet-examples/streamlet-common.h","286","FIXME","correct error codes" |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
191 |
…]]></pre> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
192 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
193 |
<p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
194 |
And we can process it using this pipeline: |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
195 |
</p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
196 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
197 |
<m:pre jazyk="bash"><![CDATA[cat tasks.csv \ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
198 |
| relpipe-in-csv \ |
325
4560c5623d7d
relpipe-tr-grep has new syntax since v0.18: --relation, --attribute, --value
František Kučera <franta-hg@frantovo.cz>
parents:
301
diff
changeset
|
199 |
| relpipe-tr-grep --relation 'csv' --attribute 'type' --value 'FIXME' \ |
326
ab7f333f1225
relpipe-tr-cut has new syntax since v0.18: --relation, --attribute
František Kučera <franta-hg@frantovo.cz>
parents:
325
diff
changeset
|
200 |
| relpipe-tr-cut --relation 'csv' --attribute 'file|description' \ |
301
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
201 |
| relpipe-out-tabular]]></m:pre> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
202 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
203 |
<p>and get result like this:</p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
204 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
205 |
<pre><![CDATA[csv: |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
206 |
╭───────────────────────────────────────┬──────────────────────╮ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
207 |
│ file (string) │ description (string) │ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
208 |
├───────────────────────────────────────┼──────────────────────┤ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
209 |
│ streamlet-examples/streamlet-common.h │ correct error codes │ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
210 |
│ streamlet-examples/streamlet-common.h │ correct error codes │ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
211 |
│ streamlet-examples/Streamlet.java │ correct error codes │ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
212 |
╰───────────────────────────────────────┴──────────────────────╯ |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
213 |
Record count: 3]]></pre> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
214 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
215 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
216 |
<p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
217 |
We work with attribute (column) names, so there is no need to remember column numbers. |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
218 |
And thanks to regular expressions we can write elegant and powerful filters. |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
219 |
</p> |
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
220 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
221 |
|
7029e6c47700
examples: improved Doing projection and restriction using cut and grep – add a chapter about CSV
František Kučera <franta-hg@frantovo.cz>
parents:
244
diff
changeset
|
222 |
|
87
25dec6931f18
Lepší odsazení, tabulátory.
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
223 |
</text> |
4
1bb39595a51c
genrování hlavní nabídky #1
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
224 |
|
1 | 225 |
</stránka> |