Skip to content

SPARQL Scripts¤

By prepending a Shebang line to a SPARQL query file and making this file executable, the query file can be treated as an executable script. As an example, here is a simple text file with a generic SPARQL query which counts all triples in all graphs and outputs an ordered list:
SELECT DISTINCT ?graph (COUNT(?graph) AS ?triples)
    GRAPH ?graph
        ?s ?p ?o
GROUP BY ?graph
ORDER BY DESC(?triples)

In order to make a SPARQL script out of this text file you need to add the following line on top of this file:

shebang line for SPARQL scripts
#!/usr/bin/env -S cmemc query execute --accept text/csv

This will set cmemc as an interpreter for the rest of the file and by using the query execute command, the rest of the file will be used as a SPARQL query.

Now you need to define your SPARQL file as executable and run it:

$ chmod a+x ./
$ ./