471 <p> |
471 <p> |
472 If we need exact match of the whole attribute, we have to use something like <code>'^btrfs|xfs$'</code>, |
472 If we need exact match of the whole attribute, we have to use something like <code>'^btrfs|xfs$'</code>, |
473 otherwise mere substring-match is enough to include the record. |
473 otherwise mere substring-match is enough to include the record. |
474 </p> |
474 </p> |
475 |
475 |
|
476 <h2>SELECT mount_point FROM fstab WHERE type IN ('btrfs', 'xfs')</h2> |
|
477 |
|
478 <p> |
|
479 While reading classic pipelines involving <code>grep</code> and <code>cut</code> commands |
|
480 we must notice that there is some similarity with simple SQL queries looking like: |
|
481 </p> |
|
482 |
|
483 <m:pre jazyk="SQL">SELECT "some", "cut", "fields" FROM stdin WHERE grep_matches(whole_line);</m:pre> |
|
484 |
|
485 <p> |
|
486 And that is true: <code>grep</code> does restriction<m:podČarou> |
|
487 <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> |
|
488 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>. |
|
489 Now we can do these relational operations using our relational tools called <code>relpipe-tr-grep</code> and <code>relpipe-tr-cut</code>. |
|
490 </p> |
|
491 |
|
492 <p> |
|
493 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> |
|
494 and we want to do something (a shell script block) with these directory paths. |
|
495 </p> |
|
496 |
|
497 <m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \ |
|
498 | relpipe-tr-grep 'fstab' 'type' '^btrfs|xfs$' \ |
|
499 | relpipe-tr-cut 'fstab' 'mount_point' \ |
|
500 | relpipe-out-nullbyte \ |
|
501 | while read -r -d '' m; do |
|
502 echo "$m"; |
|
503 done]]></m:pre> |
|
504 |
|
505 <p> |
|
506 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. |
|
507 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. |
|
508 </p> |
|
509 |
|
510 <p> |
|
511 Then we pass the data to the Bash while cycle. |
|
512 In such simple scenario (just <code>echo</code>), we could use <code>xargs</code> as in examples before, |
|
513 but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them. |
|
514 </p> |
|
515 |
|
516 <h2>More projections with relpipe-tr-cut</h2> |
|
517 |
|
518 <p> |
|
519 Assume that we have a simple relation containing numbers: |
|
520 </p> |
|
521 |
|
522 <m:pre jazyk="bash"><![CDATA[seq 0 8 \ |
|
523 | tr \\n \\0 \ |
|
524 | relpipe-in-cli generate-from-stdin numbers 3 a integer b integer c integer \ |
|
525 > numbers.rp]]></m:pre> |
|
526 |
|
527 <p>and second one containing letters:</p> |
|
528 |
|
529 <m:pre jazyk="bash"><![CDATA[relpipe-in-cli generate letters 2 a string b string A B C D > letters.rp]]></m:pre> |
|
530 |
|
531 <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> |
|
532 |
|
533 <m:pre jazyk="bash"><![CDATA[cat numbers.rp letters.rp > both.rp; |
|
534 cat both.rp | relpipe-out-tabular]]></m:pre> |
|
535 |
|
536 <p>Will print:</p> |
|
537 |
|
538 <pre><![CDATA[numbers: |
|
539 ╭─────────────┬─────────────┬─────────────╮ |
|
540 │ a (integer) │ b (integer) │ c (integer) │ |
|
541 ├─────────────┼─────────────┼─────────────┤ |
|
542 │ 0 │ 1 │ 2 │ |
|
543 │ 3 │ 4 │ 5 │ |
|
544 │ 6 │ 7 │ 8 │ |
|
545 ╰─────────────┴─────────────┴─────────────╯ |
|
546 Record count: 3 |
|
547 letters: |
|
548 ╭─────────────┬─────────────╮ |
|
549 │ a (string) │ b (string) │ |
|
550 ├─────────────┼─────────────┤ |
|
551 │ A │ B │ |
|
552 │ C │ D │ |
|
553 ╰─────────────┴─────────────╯ |
|
554 Record count: 2]]></pre> |
|
555 |
|
556 <p>We can put away the <code>a</code> attribute from the <code>numbers</code> relation:</p> |
|
557 |
|
558 <m:pre jazyk="bash">cat both.rp | relpipe-tr-cut 'numbers' 'b|c' | relpipe-out-tabular</m:pre> |
|
559 |
|
560 <p>and leave the <code>letters</code> relation unaffected:</p> |
|
561 |
|
562 <pre><![CDATA[numbers: |
|
563 ╭─────────────┬─────────────╮ |
|
564 │ b (integer) │ c (integer) │ |
|
565 ├─────────────┼─────────────┤ |
|
566 │ 1 │ 2 │ |
|
567 │ 4 │ 5 │ |
|
568 │ 7 │ 8 │ |
|
569 ╰─────────────┴─────────────╯ |
|
570 Record count: 3 |
|
571 letters: |
|
572 ╭─────────────┬─────────────╮ |
|
573 │ a (string) │ b (string) │ |
|
574 ├─────────────┼─────────────┤ |
|
575 │ A │ B │ |
|
576 │ C │ D │ |
|
577 ╰─────────────┴─────────────╯ |
|
578 Record count: 2]]></pre> |
|
579 |
|
580 <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> |
|
581 |
|
582 <m:pre jazyk="bash">cat both.rp | relpipe-tr-cut '.*' 'b|c' | relpipe-out-tabular</m:pre> |
|
583 |
|
584 <p>Instead of <code>'.*'</code> we could use <code>'numbers|letters'</code> and in this case it will give the same result:</p> |
|
585 |
|
586 <pre><![CDATA[numbers: |
|
587 ╭─────────────┬─────────────╮ |
|
588 │ b (integer) │ c (integer) │ |
|
589 ├─────────────┼─────────────┤ |
|
590 │ 1 │ 2 │ |
|
591 │ 4 │ 5 │ |
|
592 │ 7 │ 8 │ |
|
593 ╰─────────────┴─────────────╯ |
|
594 Record count: 3 |
|
595 letters: |
|
596 ╭─────────────╮ |
|
597 │ b (string) │ |
|
598 ├─────────────┤ |
|
599 │ B │ |
|
600 │ D │ |
|
601 ╰─────────────╯ |
|
602 Record count: 2]]></pre> |
|
603 |
|
604 <p>All the time, we are reducing the attributes. But we can also multiply them or change their order:</p> |
|
605 |
|
606 <m:pre jazyk="bash">cat both.rp | relpipe-tr-cut 'numbers' 'b|a|c' 'b' 'a' 'a' | relpipe-out-tabular</m:pre> |
|
607 |
|
608 <p> |
|
609 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; |
|
610 but if we specify multiple regexes to specify attributes, their order and count matters: |
|
611 </p> |
|
612 |
|
613 <pre><![CDATA[numbers: |
|
614 ╭─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────╮ |
|
615 │ a (integer) │ b (integer) │ c (integer) │ b (integer) │ a (integer) │ a (integer) │ |
|
616 ├─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┤ |
|
617 │ 0 │ 1 │ 2 │ 1 │ 0 │ 0 │ |
|
618 │ 3 │ 4 │ 5 │ 4 │ 3 │ 3 │ |
|
619 │ 6 │ 7 │ 8 │ 7 │ 6 │ 6 │ |
|
620 ╰─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────╯ |
|
621 Record count: 3 |
|
622 letters: |
|
623 ╭─────────────┬─────────────╮ |
|
624 │ a (string) │ b (string) │ |
|
625 ├─────────────┼─────────────┤ |
|
626 │ A │ B │ |
|
627 │ C │ D │ |
|
628 ╰─────────────┴─────────────╯ |
|
629 Record count: 2]]></pre> |
|
630 |
|
631 <p> |
|
632 The <code>letters</code> relation stays rock steady and <code>relpipe-tr-cut 'numbers'</code> does not affect it in any way. |
|
633 </p> |
|
634 |
|
635 |
476 </text> |
636 </text> |
477 |
637 |
478 </stránka> |
638 </stránka> |