relpipe-data/examples/awk-through-xml.sh
author František Kučera <franta-hg@frantovo.cz>
Fri, 25 Sep 2020 14:38:24 +0200
branchv_0
changeset 316 d7ae02390fac
parent 288 5cf3a702f47d
permissions -rwxr-xr-x
relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp

#!/bin/bash

getXML() {
	# Just read a file:
	cat "/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf"
	# Or we can wget or curl it from the network
	# or generate on-the-fly using some other command.
}

parseXML() {
	# Convert the XML tree structure into multiple relations.
	# Use XPath expressions to find record nodes
	# and to find attributes inside them.
	relpipe-in-xmltable \
		--relation 'policy' \
			--records '/busconfig/policy/allow|/busconfig/policy/deny' \
			--attribute 'policy'           string 'name()' \
			--attribute 'user'             string '../@user' \
			--attribute 'context'          string '../@context' \
			--attribute 'own'              string '@own' \
			--attribute 'send_destination' string '@send_destination' \
			--attribute 'send_interface'   string '@send_interface' \
			--attribute 'send_member'      string '@send_member' \
		--relation 'limit' \
			--records '/busconfig/limit' \
			--attribute 'name'             string '@name' \
			--attribute 'value'            integer '.'
}

filterRecords() {
	# Use native AWK command (called from relpipe-tr-awk)
	# with all its power to filter the records.
	# Work with named fields instead of numbered columns.
	relpipe-tr-awk \
		--relation "policy" \
		--where 'policy == "allow" && user != "root"'

	# Of course, we can do the same using SQL:
	# relpipe-tr-sql \
	#	--relation "policy" \
	#		"SELECT * FROM policy WHERE policy = 'allow' AND user <> 'root'"
	#	--copy 'limit'

	# Or using Scheme:
	# relpipe-tr-scheme \
	#	--relation policy \
	#	--where '(and (string= $policy "allow") (not (string= $user "root")) )'
}

formatOutput() {
	relpipe-out-tabular
	# Or generate some other format:
	# relpipe-out-ods       # spreadsheet for e.g. LibreOffice
	# relpipe-out-xml       # XML e.g. for further XSLT or XQuery processing
	# relpipe-out-recfile   # recfile for further GNU Recutils processing
	# relpipe-out-asn1      # ASN.1 BER for the telco guys
	# relpipe-out-gui       # display data in a GUI window
	# etc.
}

# put the whole pipeline together:
getXML | parseXML | filterRecords | formatOutput