relpipe-data/examples-awk-aggregations.xml
author František Kučera <franta-hg@frantovo.cz>
Mon, 03 Feb 2020 22:10:07 +0100
branchv_0
changeset 294 abbc9bcfbcc4
parent 258 2868d772c27e
permissions -rw-r--r--
Release v0.15 – streamlets, parallel processing

<stránka
	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">

	<nadpis>Aggregating data with AWK</nadpis>
	<perex>counting records and computing sum or appending new records</perex>
	<m:pořadí-příkladu>02600</m:pořadí-příkladu>

	<text xmlns="http://www.w3.org/1999/xhtml">
		
		<p>
			We have filtered records, modified attribute values, added and removed attributes, dropped a relation… 
			and there is one more operation that we can do with AWK: <code>INSERT</code> resp. appending or preppending additional records to the relation
			– and we can also completely replace the record set by skipping the original records.
		</p>
		
		<h2>Adding records</h2>
		
		<p>
			Using options <code>--before-records</code> and <code>--after-records</code> we can pass additional AWK code that will be executed – once for given relation.
			The <code>record()</code> function will then generate an additional record (can be called multiple times and generate more records):
		</p>
		
		<m:pre jazyk="bash"><![CDATA[awkCode='
	scheme = "";
	device = "/dev/vg1/volume123";
	mount_point = "/mnt/v123";
	type = "btrfs";
	options = "relatime";
	dump = 0;
	pass = 2;

	record();
';

relpipe-in-fstab \
	| relpipe-tr-awk \
		--relation '.*' \
			--for-each '1' \
			--after-records "$awkCode" \
	| relpipe-out-tabular]]></m:pre>
	
		<p>Which will <code>INSERT</code> one new record:</p>
	
		<pre><![CDATA[fstab:
 ╭─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬───────────────────────────────────────┬────────────────┬────────────────╮
 │ scheme (string) │ device                      (string) │ mount_point (string) │ type (string) │ options                      (string) │ dump (integer) │ pass (integer) │
 ├─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼───────────────────────────────────────┼────────────────┼────────────────┤
 │ UUID            │ 29758270-fd25-4a6c-a7bb-9a18302816af │ /                    │ ext4          │ relatime,user_xattr,errors=remount-ro │              0 │              1 │
 │                 │ /dev/sr0                             │ /media/cdrom0        │ udf,iso9660   │ user,noauto                           │              0 │              0 │
 │                 │ /dev/sde                             │ /mnt/data            │ ext4          │ relatime,user_xattr,errors=remount-ro │              0 │              2 │
 │ UUID            │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home                │ btrfs         │ relatime                              │              0 │              2 │
 │                 │ /dev/mapper/sdf_crypt                │ /mnt/private         │ xfs           │ relatime                              │              0 │              2 │
 │                 │ /dev/vg1/volume123                   │ /mnt/v123            │ btrfs         │ relatime                              │              0 │              2 │
 ╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯
Record count: 6]]></pre>

		<h2>Counting and summarizing values</h2>

		<p>We can also compute some statistics like <code>COUNT()</code> and <code>SUM()</code>:</p>
		
		<m:pre jazyk="bash"><![CDATA[find -print0 | relpipe-in-filesystem \
	| relpipe-tr-awk \
		--relation '.*' \
			--before-records 'count = 0; total_size = 0;' \
			--for-each       '{ count++; total_size += size; }' \
			--after-records  'record();' \
			--output-attribute count      integer \
			--output-attribute total_size integer \
	| relpipe-out-tabular]]></m:pre>
	
		<p>and get result:</p>
	
		<pre><![CDATA[filesystem:
 ╭─────────────────┬──────────────────────╮
 │ count (integer) │ total_size (integer) │
 ├─────────────────┼──────────────────────┤
 │               9 │               818747 │
 ╰─────────────────┴──────────────────────╯
Record count: 1]]></pre>

		<p>Where the <code>total_size</code> is the same as will <code>du</code> compute:</p>
		
		<pre>find . -type f -print0 | du -b -c --files0-from=-</pre>

		<p>Analogously we can compute minimum, maximum etc. using AWK transformation.</p>

	</text>
	
</stránka>