Cfpeek User Manual (split by section):   Section:   Chapter:FastBack: Tutorial   Up: Tutorial   FastForward: Formats   Contents: Table of ContentsIndex: Concept Index

3.8 Specifying Nodes to Output

Sometimes you may need to see not the node which matched the search key, but its parent or other ancestor node. Consider, for example, the following task: select from the /etc/named.conf file the names of all zones for which this nameserver is a master. To do so, you will need to find all ‘zone.type’ statements with the value ‘master’, ascend to the parent node and print its value.

Cfpeek provides several special formatting flags to that effect: up, down, parent, child and sibling. They are called relative movement flags, because they select another node in the tree, relative to the position of the current node.

The up flag takes an integer number as its argument. It instructs cfpeek to ascend that many parent nodes before actually printing the node. For example, --format=up=1 means “ascend to the parent of the matched node and print it”. This is exactly what we need to solve the above task, since the ‘type’ statement is a child of a ‘zone’ statement. Thus, the solution is:

cfpeek --format=up=1,nodescend,value --parser=bind \
       /etc/named.conf .*.type=master

The value flag indicates that we want on output only values, without the corresponding pathnames. The nodescend flag tells cfpeek to not descend into compound statements when outputting them. It is necessary since we want only values of all relevant ‘zone’ statements, no their subordinate statements.

A counterpart of this flag is down=n flag, which descends n levels of hierarchy.

The parent flag acts in the similar manner, but it identifies the ancestor by its keyword, instead of the relative nesting level. The statement

--format=parent=zone

tells cfpeek, after finding a matching node, to ascend until a node with the identifier ‘zone’ is found, and then print this node.

The child=id statement does the opposite of parent: it locates a child of the current node which has the identifier id.

Similarly, the sibling keyword instructs cfpeek to find first sibling of the current node wich has the given identifier. For example, to find names of the zone files for all master nodes in the named.conf file:

cfpeek --parser bind --format=sibling=file,value /etc/named.conf \
       '.*.zone.type=master'

A ‘file’ statement is located on the same nesting level as ‘type’, for example:

zone "example.net" {
        type master;
        file "db.example.net";
};

Thus, the above command first locates the ‘type’ statement, then searches on the same nesting level for a ‘file’ statement, and finally prints its value.

Cfpeek User Manual (split by section):   Section:   Chapter:FastBack: Tutorial   Up: Tutorial   FastForward: Formats   Contents: Table of ContentsIndex: Concept Index