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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
288
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
getXML() {
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
	# Just read a file:
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
	cat "/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf"
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
	# Or we can wget or curl it from the network
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
	# or generate on-the-fly using some other command.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
}
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
parseXML() {
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
	# Convert the XML tree structure into multiple relations.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
	# Use XPath expressions to find record nodes
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
	# and to find attributes inside them.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
	relpipe-in-xmltable \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
		--relation 'policy' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
			--records '/busconfig/policy/allow|/busconfig/policy/deny' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
			--attribute 'policy'           string 'name()' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
			--attribute 'user'             string '../@user' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
			--attribute 'context'          string '../@context' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
			--attribute 'own'              string '@own' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
			--attribute 'send_destination' string '@send_destination' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
			--attribute 'send_interface'   string '@send_interface' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
			--attribute 'send_member'      string '@send_member' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
		--relation 'limit' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
			--records '/busconfig/limit' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
			--attribute 'name'             string '@name' \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
			--attribute 'value'            integer '.'
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
}
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
filterRecords() {
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	# Use native AWK command (called from relpipe-tr-awk)
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	# with all its power to filter the records.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	# Work with named fields instead of numbered columns.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	relpipe-tr-awk \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		--relation "policy" \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		--where 'policy == "allow" && user != "root"'
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	# Of course, we can do the same using SQL:
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	# relpipe-tr-sql \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	#	--relation "policy" \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	#		"SELECT * FROM policy WHERE policy = 'allow' AND user <> 'root'"
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	#	--copy 'limit'
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
316
d7ae02390fac relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents: 288
diff changeset
    44
	# Or using Scheme:
d7ae02390fac relpipe-tr-guile.cpp → relpipe-tr-scheme.cpp
František Kučera <franta-hg@frantovo.cz>
parents: 288
diff changeset
    45
	# relpipe-tr-scheme \
288
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	#	--relation policy \
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	#	--where '(and (string= $policy "allow") (not (string= $user "root")) )'
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
}
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
formatOutput() {
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	relpipe-out-tabular
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	# Or generate some other format:
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	# relpipe-out-ods       # spreadsheet for e.g. LibreOffice
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	# relpipe-out-xml       # XML e.g. for further XSLT or XQuery processing
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	# relpipe-out-recfile   # recfile for further GNU Recutils processing
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	# relpipe-out-asn1      # ASN.1 BER for the telco guys
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	# relpipe-out-gui       # display data in a GUI window
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	# etc.
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
}
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
# put the whole pipeline together:
5cf3a702f47d examples: AWKing through a XML file
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
getXML | parseXML | filterRecords | formatOutput