# HG changeset patch # User František Kučera # Date 1544705886 -3600 # Node ID bf9a704dc9169b108cd2e48aa480a7fa8b0c4c1a # Parent e5afc8d8326846bc3dba7a44d85d62283f4c336e examples: relpipe-tr-cut diff -r e5afc8d83268 -r bf9a704dc916 relpipe-data/examples.xml --- a/relpipe-data/examples.xml Thu Dec 13 01:32:00 2018 +0100 +++ b/relpipe-data/examples.xml Thu Dec 13 13:58:06 2018 +0100 @@ -473,6 +473,166 @@ otherwise mere substring-match is enough to include the record.

+

SELECT mount_point FROM fstab WHERE type IN ('btrfs', 'xfs')

+ +

+ While reading classic pipelines involving grep and cut commands + we must notice that there is some similarity with simple SQL queries looking like: +

+ + SELECT "some", "cut", "fields" FROM stdin WHERE grep_matches(whole_line); + +

+ And that is true: grep does restriction + selecting only certain records from the original relation according to their match with given conditions + and cut does projectionlimited subset of what projection means. + Now we can do these relational operations using our relational tools called relpipe-tr-grep and relpipe-tr-cut. +

+ +

+ Assume that we need only mount_point fields from our fstab where type is btrfs or xfs + and we want to do something (a shell script block) with these directory paths. +

+ + + +

+ The relpipe-tr-cut tool has similar syntax to its grep and sed siblings and also uses the power of regular expressions. + In this case it modifies on-the-fly the fstab relation and drops all its attributes except the mount_point one. +

+ +

+ Then we pass the data to the Bash while cycle. + In such simple scenario (just echo), we could use xargs as in examples before, + but in this syntax, we can write whole block of shell commands for each record/value and do more complex actions with them. +

+ +

More projections with relpipe-tr-cut

+ +

+ Assume that we have a simple relation containing numbers: +

+ + numbers.rp]]> + +

and second one containing letters:

+ + letters.rp]]> + +

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:

+ + both.rp; +cat both.rp | relpipe-out-tabular]]> + +

Will print:

+ +
+ +

We can put away the a attribute from the numbers relation:

+ + cat both.rp | relpipe-tr-cut 'numbers' 'b|c' | relpipe-out-tabular + +

and leave the letters relation unaffected:

+ +
+ +

Or we can remove a from both relations resp. keep there only attributes whose names match 'b|c' regex:

+ + cat both.rp | relpipe-tr-cut '.*' 'b|c' | relpipe-out-tabular + +

Instead of '.*' we could use 'numbers|letters' and in this case it will give the same result:

+ +
+ +

All the time, we are reducing the attributes. But we can also multiply them or change their order:

+ + cat both.rp | relpipe-tr-cut 'numbers' 'b|a|c' 'b' 'a' 'a' | relpipe-out-tabular + +

+ n.b. the order in 'b|a|c' does not matter and if such regex matches, it preserves the original order of the attributes; + but if we specify multiple regexes to specify attributes, their order and count matters: +

+ +
+ +

+ The letters relation stays rock steady and relpipe-tr-cut 'numbers' does not affect it in any way. +

+ +