macoshomebrew

How to get the version of each installed formula in Homebrew?


We are trying to get the version of every package installed.

Can anyone please help to provide brew command that will print like this?

Name            Installed     Latest
ant               1.9.4        1.9.4
maven             3.6.3        3.6.3
mariadb@10.2      10.2.31.     10.2.37
node              v12.1.0      v14.0

This is command tried:

brew list --formula | xargs -n1 -P8 -I {} sh -c "brew info {}| egrep '[0-9]* stable'|sed -e 's/stable//g' -e 's/HEAD//g' -e 's/(bottled)//g' -e 's/,//g' -e 's/\[keg-only\]//g' -e 's/://g'"|egrep 'node|maven|ant|mariadb'|grep -v "Warning"|column -t

And I am getting only this:

ant      1.10.9
maven    3.6.3
node     15.12.0
node@14  14.16.0

Need advise on how to retrieve the latest version from public and append?


Solution

  • Use the --json option of brew info to get the formula information as JSON and use jq to extract the version from it. It is safer and easier than using grep, aws or other tools that are oriented on lines.

    Use brew install jq to install jq if it is not already installed :-)

    brew list --formulae |
    xargs brew info --json |
    jq -r '
        ["name", "latest", "installed version(s)"],
        (.[] | [ .name, .versions.stable, (.installed[] | .version) ])
        | @tsv
    '
    

    The command above lists all installed formulae, passes their names to brew info then extracts the needed information from the JSON output produced by brew info --json.

    It produces a list of tab-separated values with the following columns: name, latest and installed versions(s). The order of the columns is swapped because it is possible to have multiple versions of formulae installed locally. When this happens, the installed versions are produced in separate columns, starting with column #3.

    The output can be redirected to a file and then imported in MS Excel/OpenOffice/LibreOffice/Google Sheets or it can be processed automatically using simple shell commands (cut, grep etc).

    The jq script

    (
      ["name", "latest", "installed version(s)"]
      ,
      (.[] | [ .name, .versions.stable, (.installed[] | .version) ])
    )
    | @tsv
    

    The 1st and 5th line of the JQ script listed here (the parentheses) are not present in the complete command line because they are not really needed. They are present here for explanatory purposes but the precedence of the JQ operators ensures the correct execution even without them.

    The 2nd line produces an array containing the three string literals presented here; they represent the first line of the final output (the header). It does not use the input data.

    The comma on the 3rd line sends the input data to the first filter (line 2) then to the second filter (line 4) then concatenates their output.

    The 4th line iterates through the input array (the output of brew info is an array of objects) and sends each item (|) to the subsequent filter that extracts the fields name, versions.stable and version from all values of the installed array) into an array [ ... ]. It generates an array containing three or more entries for each formula.

    The parentheses on the 1st and 5th line group the output of the filters described above and pass the result (|) to the next filter.

    The @tsv operator on the 6th line transforms each array generated above into a string that contains the values separated by comma.

    The -r in the command line of JQ tells it to produce raw output. This means that, if possible, to not produce JSON but plain text. Without it the output of the script is the value encoded as JSON for each formula (f.e. "jq\t1.6\t1.6" literally). With it the output is just the value of the string without any encoding or escaping or other JSON conventions.

    If multiple versions of a formula are installed, the script generates 4 or more values on its line. The 3rd and the subsequent values are the versions installed locally.

    Disclaimer

    I didn't test the script with multiple versions of a formula installed, I hope it works as described. I don't have multiple versions of any formula and I don't know if it is still possible to install multiple versions of a formula now. It was possible in the past and brew list can display them, if the are. Just run brew list --multiple --versions.