relpipe-data/examples/guile-file-count-size-sum.sh
author František Kučera <franta-hg@frantovo.cz>
Sat, 26 Oct 2019 20:57:42 +0200
branchv_0
changeset 276 cfaf1fa94154
parent 248 e76ca9f7d6cb
child 316 d7ae02390fac
permissions -rwxr-xr-x
rename --keep-file to --file-keep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
248
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# argument: directory path
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# prints file count and sum of file sizes
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
find "$1" -type f -print0 \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
	| relpipe-in-filesystem \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
		--file path \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
		--file size \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
	| relpipe-tr-guile \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
		--relation 'f.*' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
		--output-attribute 'count' integer \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
		--output-attribute 'sum'   integer \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
		--before-records '
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
			(define $sum   0)
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
			(define $count 0)
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
			(define return-sum #f)' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
		--for-each '
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
			(set! $sum   (+ $sum   $size) )
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
			(set! $count (+ $count 1    ) )' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
		--where '#f' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
		--after-records '(set! return-sum #t)' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
		--has-more-records '
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
			(if return-sum
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
				(begin (set! return-sum #f) #t)
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
				#f
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
			)' \
e76ca9f7d6cb examples: Aggregating data with Guile
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
	| relpipe-out-tabular