relpipe-data/examples-awk-changing-values.xml
author František Kučera <franta-hg@frantovo.cz>
Sat, 26 Oct 2019 20:57:42 +0200
branchv_0
changeset 276 cfaf1fa94154
parent 258 2868d772c27e
permissions -rw-r--r--
rename --keep-file to --file-keep

<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>Changing values with AWK</nadpis>
	<perex>regular expression text replacement</perex>
	<m:pořadí-příkladu>02300</m:pořadí-příkladu>

	<text xmlns="http://www.w3.org/1999/xhtml">
		
		<p>
			Besides filtering, we can use an AWK transformation to modify attribute values.
			This means simply rewriting the value of given variable in AWK and calling <code>record()</code> function at the end.
		</p>
		
		<p>For example we can move all volumes mounted under <code>/mnt/</code> to another directory using regular expressions:</p>
		
		<m:pre jazyk="bash"><![CDATA[relpipe-in-fstab \
	| relpipe-tr-awk \
		--relation '.*' \
			--for-each '{ mount_point = gensub("^/mnt/", "/mnt/old/", "g", mount_point); record(); }' \
	| relpipe-out-tabular]]></m:pre>

		<p>which will result in:</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/old/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/old/private     │ xfs           │ relatime                              │              0 │              2 │
 ╰─────────────────┴──────────────────────────────────────┴──────────────────────┴───────────────┴───────────────────────────────────────┴────────────────┴────────────────╯
Record count: 5]]></pre>

		<p>
			We can modify multiple attributes in a single transformation
			and we can also use other AWK functions like <code>toupper()</code>, <code>tolower()</code> etc.
		</p>

	</text>
	
</stránka>