The AWK transformations can also change the structure of transformed relation. It means adding or removing attributes or dropping the whole relation.
Using --output-attribute
we can specify the output attributes.
If we do not want to explicitly specify all of them and just want to add some new ones, we will use --input-attributes-append
(or --input-attributes-prepend
),
which will preserve also the input attributes:
relpipe-in-fstab \
| relpipe-tr-awk \
--relation '.*' \
--for-each '{ id = NR; record(); }' \
--output-attribute id integer \
--input-attributes-append \
| relpipe-out-tabular
This adds one new attribute with ordinal numbers:
fstab: ╭──────────────┬─────────────────┬──────────────────────────────────────┬──────────────────────┬───────────────┬───────────────────────────────────────┬────────────────┬────────────────╮ │ id (integer) │ scheme (string) │ device (string) │ mount_point (string) │ type (string) │ options (string) │ dump (integer) │ pass (integer) │ ├──────────────┼─────────────────┼──────────────────────────────────────┼──────────────────────┼───────────────┼───────────────────────────────────────┼────────────────┼────────────────┤ │ 1 │ UUID │ 29758270-fd25-4a6c-a7bb-9a18302816af │ / │ ext4 │ relatime,user_xattr,errors=remount-ro │ 0 │ 1 │ │ 2 │ │ /dev/sr0 │ /media/cdrom0 │ udf,iso9660 │ user,noauto │ 0 │ 0 │ │ 3 │ │ /dev/sde │ /mnt/data │ ext4 │ relatime,user_xattr,errors=remount-ro │ 0 │ 2 │ │ 4 │ UUID │ a2b5f230-a795-4f6f-a39b-9b57686c86d5 │ /home │ btrfs │ relatime │ 0 │ 2 │ │ 5 │ │ /dev/mapper/sdf_crypt │ /mnt/private │ xfs │ relatime │ 0 │ 2 │ ╰──────────────┴─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯ Record count: 5
Or we can omit omit attributes unless explicitly specified ones:
relpipe-in-fstab \
| relpipe-tr-awk \
--relation '.*' \
--for-each '{ type_big = toupper(type); record(); }' \
--output-attribute mount_point string \
--output-attribute type string \
--output-attribute type_big string \
| relpipe-out-tabular
which effectively removes unlisted attributes:
fstab: ╭──────────────────────┬───────────────┬───────────────────╮ │ mount_point (string) │ type (string) │ type_big (string) │ ├──────────────────────┼───────────────┼───────────────────┤ │ / │ ext4 │ EXT4 │ │ /media/cdrom0 │ udf,iso9660 │ UDF,ISO9660 │ │ /mnt/data │ ext4 │ EXT4 │ │ /home │ btrfs │ BTRFS │ │ /mnt/private │ xfs │ XFS │ ╰──────────────────────┴───────────────┴───────────────────╯ Record count: 5
AWK is a powerful language so we can use conditions, for cycles etc. and write much more complex transformations.
A relation can be „dropped“ which means that transformation will run but no relational output will be generated for it
(even the header will be omitted, so it differs from just eliminating all records by a condition).
Using AWK for such a simple operation like DROP
seems weird but sometimes it might make sense due to intentional side effects.
Because the AWK code is executed for each record, we can e.g. write some output to a file or to STDERR:
relpipe-in-fstab \
| relpipe-tr-awk \
--relation '.*' \
--for-each '{ printf("%s → %s\n", device, mount_point) > "/dev/stderr" }' \
--drop
Which prints text:
29758270-fd25-4a6c-a7bb-9a18302816af → / /dev/sr0 → /media/cdrom0 /dev/sde → /mnt/data a2b5f230-a795-4f6f-a39b-9b57686c86d5 → /home /dev/mapper/sdf_crypt → /mnt/private
Then relpipe-tr-awk
works much like an output filter (converts relational data to another format).
However, if there are more relations and some of theme are not matched by --relation
, they will be passed through and delivered to the STDOUT in the relational format.
STDERR might be occasionally polluted by some warning messages, so using a dedicated file for such output is a safer way.
Relational pipes, open standard and free software © 2018-2022 GlobalCode