OCI Commandline Interface (CLI) Filtering

In my previous CLI post, I went over some of the basics of getting started with the command line client (CLI) for the Oracle Cloud Infrastructure (OCI).

In this post, I’ll dive a little deeper and show how you can filter a result set, and limit the display to certain fields.

In this example, I want to find the OCID for a certain compartment, of which I only remember the name. I also want to limit the output to just the OCID:

The query parameter uses the JMES path syntax. The element data refers to the root array of the CLI output. The query is contained within the array’s square brackets. The typical comparison operators are available and can be reviewed in the documentation.
Note that when referring to fields with hyphens in them, the hyphen is interpreted as a subtraction operator. To work around this issue, these fields must be enclosed in escaped double quotes:

To make the output easier to read, you may want to opt for a tabular output using the –output parameter:
Here is an example that only gets instances that contain a defined tag named Schedule:

Now here is a fun one: List the Linux images, version > 7, that do not have “GPU” in their display name. This uses the contains() function. Note that if you want to do a NOT contains, you have to add the expression == false (yes, those are backticks or grave accent marks)
Currently, the –output options are limited to json and table, with json being the default.
Please let me know in the comments below if you find some cool ways of using the OCI CLI.

Leave a Reply

Your email address will not be published. Required fields are marked *