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: INSERT
resp. appending or preppending additional records to the relation
– and we can also completely replace the record set by skipping the original records.
Using options --before-records
and --after-records
we can pass additional AWK code that will be executed – once for given relation.
The record()
function will then generate an additional record (can be called multiple times and generate more records):
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
Which will INSERT
one new record:
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
We can also compute some statistics like COUNT()
and SUM()
:
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
and get result:
filesystem: ╭─────────────────┬──────────────────────╮ │ count (integer) │ total_size (integer) │ ├─────────────────┼──────────────────────┤ │ 9 │ 818747 │ ╰─────────────────┴──────────────────────╯ Record count: 1
Where the total_size
is the same as will du
compute:
find . -type f -print0 | du -b -c --files0-from=-
Analogously we can compute minimum, maximum etc. using AWK transformation.
Relational pipes, open standard and free software © 2018-2022 GlobalCode