diff -r 9c1d0c5ed599 -r d4f401b5f90c relpipe-data/examples-grep-cut-fstab.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples-grep-cut-fstab.xml Tue Feb 05 19:18:28 2019 +0100 @@ -0,0 +1,170 @@ + + + Doing projection and restriction using cut and grep + SELECT mount_point FROM fstab WHERE type IN ('btrfs', 'xfs') + 01000 + + + +

+ 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 above, + 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 use 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. +

+ +
+ +