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