|
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>Classic pipeline example</nadpis> |
|
6 <perex>Explained example of classic pipeline</perex> |
|
7 |
|
8 <text xmlns="http://www.w3.org/1999/xhtml"> |
|
9 <p> |
|
10 Assume that we have a text file containing a list of animals and their properties: |
|
11 </p> |
|
12 |
|
13 <m:pre src="animals.txt"/> |
|
14 |
|
15 <p> |
|
16 We can pass this file through a pipeline: |
|
17 </p> |
|
18 |
|
19 <m:classic-example/> |
|
20 |
|
21 <p> |
|
22 Particular steps of the pipeline are separated by the | pipe symbol. |
|
23 In the first step, we just read the file and print it on STDOUT.<m:podČarou>Of course, this is an <a href="http://porkmail.org/era/unix/award.html" title="Useless Use of Cat">UUoC</a>, but in examples the right order makes it easier to read than usage of < file redirections.</m:podČarou> |
|
24 In the second step, we filter only dogs and get: |
|
25 </p> |
|
26 |
|
27 <pre><![CDATA[big yellow dog |
|
28 small white dog]]></pre> |
|
29 |
|
30 <p> |
|
31 In the third step, we select second <em>field</em> (fields are separated by spaces) and get colours of our dogs: |
|
32 </p> |
|
33 |
|
34 <pre><![CDATA[yellow |
|
35 white]]></pre> |
|
36 |
|
37 <p> |
|
38 In the fourth step, we translate the values to uppercase and get: |
|
39 </p> |
|
40 |
|
41 <pre><![CDATA[YELLOW |
|
42 WHITE]]></pre> |
|
43 |
|
44 <p> |
|
45 So we have a list of colors of our dogs printed upper-case. |
|
46 In case we have several dogs of same colors, we could avoid duplicates simply by adding <code>| sort -u</code> in the pipeline (after the <code>cut</code> part). |
|
47 </p> |
|
48 |
|
49 <h2>The great parts</h2> |
|
50 |
|
51 <p> |
|
52 The authors of <code>cat</code>, <code>grep</code>, <code>cut</code> or <code>tr</code> programs don't have to know anything about cats<m:podČarou>n.b. the cat in the command name is a different cat than in our text file</m:podČarou> and dogs and our business domain. |
|
53 They can focus on their tasks which are reading files, filtering by regular expressions, doing some substrings and text conversions. And they do it well without being distracted by any animals. |
|
54 </p> |
|
55 |
|
56 <p> |
|
57 And we don't have to know anything about the low-level programming in the C language or compile anything. |
|
58 We just simply build a pipeline in a shell (e.g. GNU Bash) from existing programs and focus on our business logic. |
|
59 And we do it well without being distracted by any low-level issues. |
|
60 </p> |
|
61 |
|
62 <h2>The pitfalls</h2> |
|
63 |
|
64 <p> |
|
65 This simple example looks quite flawlessly. |
|
66 But actually it is very brittle. |
|
67 </p> |
|
68 |
|
69 <p> |
|
70 What if we have a very big cat that can be described by this line in our file? |
|
71 </p> |
|
72 |
|
73 <pre>dog-sized red cat</pre> |
|
74 |
|
75 <p>In the second step of the pipeline (<code>grep</code>) we will include this record and the final result will be:</p> |
|
76 |
|
77 <pre><![CDATA[RED |
|
78 YELLOW |
|
79 WHITE]]></pre> |
|
80 |
|
81 <p>Which is really unexpected and unwanted result. We don't have a RED dog and this is just an accident. The same would happen if we have a monkey of a <em>doggish</em> color.</p> |
|
82 |
|
83 <p> |
|
84 This problem is caused by the fact that the <code>grep dog</code> filters lines containing the word <em>dog</em> regardless its position (first, second or third field). |
|
85 Sometimes we could avoid such problems by a bit more complicated regular expression and/or by using Perl, but our pipeline wouldn't be as simple and legible as before. |
|
86 </p> |
|
87 |
|
88 <p> |
|
89 What if we have a turtle that has lighter color than other turtles? |
|
90 </p> |
|
91 |
|
92 <pre>small light green turtle</pre> |
|
93 |
|
94 <p> |
|
95 If we do <code>grep turtle</code> it will work well in this case, but our pipeline will fail in the third step where the <code>cut</code> will select only <em>light</em> (instead of <em>light green</em>). |
|
96 And the final result will be: |
|
97 </p> |
|
98 |
|
99 <pre><![CDATA[GREEN |
|
100 LIGHT]]></pre> |
|
101 |
|
102 <p> |
|
103 Which is definitively wrong because the second turtle is not LIGHT, it is LIGHT GREEN. |
|
104 This problem is caused by the fact that we don't have a well-defined separators between fields. |
|
105 Sometimes we could avoid such problems by restrictions/presumptions e.g. <em>the color must not contain a space character</em> (we could replace spaces by hyphens). |
|
106 Or we could use some other field delimiter e.g. ; or | or ,. But still we would not be able to use such character in the field values. |
|
107 So we must invent some kind of escaping (like <code>\;</code> is not a separator but a part of the field value) |
|
108 or add some quotes/apostrophes (which still requires escaping, because what if we have e.g. name field containing an apostrophe?). |
|
109 And parsing such inputs by classic tools and regular expressions is not easy and sometimes even not possible. |
|
110 </p> |
|
111 |
|
112 <p> |
|
113 There are also other problems like character encoding, missing meta-data (e.g. field names and types), joining multiple files (Is there always a new-line character at the end of the file? Or is there a BOM at the beginning of the file?) |
|
114 or passing several types of data in a single stream (we have list of animals and we can have e.g. also a list of foods or list of our staff where each list has different fields). |
|
115 </p> |
|
116 |
|
117 </text> |
|
118 |
|
119 </stránka> |