I'm trying to write a script that checks specific information of the conda that's installed on the user's computer.
For example, consider the call of conda info
below. For my use, I just need the type of conda (miniforge3), the channel URLs, and the platform.
How would someone go about getting specific information from a data dump like this?
Is there a way to parse this data in a bash script?
If not, is there someway I can call something like conda info.platform
and have it echo osx-arm64
?
higgsy@Higgsys-MBP Desktop % conda info
active environment : None
shell level : 0
user config file : /Users/higgsy/.condarc
populated config files : /Users/higgsy/miniforge3/.condarc
/Users/higgsy/.condarc
conda version : 4.12.0
conda-build version : not installed
python version : 3.9.13.final.0
virtual packages : __osx=12.5=0
__unix=0=0
__archspec=1=arm64
base environment : /Users/higgsy/miniforge3 (writable)
conda av data dir : /Users/higgsy/miniforge3/etc/conda
conda av metadata url : None
channel URLs : https://conda.anaconda.org/conda-forge/osx-arm64
https://conda.anaconda.org/conda-forge/noarch
package cache : /Users/higgsy/miniforge3/pkgs
/Users/higgsy/.conda/pkgs
envs directories : /Users/higgsy/miniforge3/envs
/Users/higgsy/.conda/envs
platform : osx-arm64
user-agent : conda/4.12.0 requests/2.27.1 CPython/3.9.13 Darwin/21.6.0 OSX/12.5
UID:GID : 501:20
netrc file : None
offline mode : False
CodePudding user response:
I solved it with the help of @j_b and @Larme.
If you want to isolate something from conda info
, you can pipe it into grep
and awk
.
For example, the example below finds the line with platform
in it with grep and then removes platform :
with awk
using sub
conda info | grep 'platform' | awk '{sub(/platform.:./,""); print}'
osx-arm64
Similarly, the channel URLs
can be found the same way.
conda info | grep 'channel URLs' | awk '{sub(/channel.URLs.:./,""); print}'
https://conda.anaconda.org/conda-forge/osx-arm64
Thanks again, @Larme and @j_b
CodePudding user response:
conda info | mawk '_{exit}_ =!_<(NF=NF)' OFS= FS='^. platform[ :] '
osx-arm64
CodePudding user response:
Avoid parsing formatted output
It is better not to rely on processing formatted shell output. This particular output from Conda is meant for users to read, not to be programmatically parsed. As such, it is subject to change.
Programmatic formats
JSON
Instead, one can obtain all the same information formatted as JSON with conda info --json
. This will give even more information and may be overkill, but it will be reliably formatted. I would go this route if the processing is not happening in shell, but in a full-featured scripting language (e.g., Python, JavaScript).
YAML, with specific queries
Alternatively, the conda config --show
command can query an arbitrary set of configuration values. For example, if you wanted just the platform, it goes by subdir
:
> conda config --show subdir
subdir: osx-64
or both platform and channels:
> conda config --show subdir channels
subdir: osx-64
channels:
- conda-forge
- bioconda
- defaults
Note that this output is all valid YAML.
Conda "type" is not a thing
The Conda configuration does not have a setting for the installer of origin. Moreover, all Conda installations can effectively be converted to another variant with a matter of package installs and adding settings to .condarc
.
For example, I have an old Miniconda3 installation on a machine, but it is essentially a Mambaforge installation, since conda-forge
channel is prioritized and mamba
is installed. I'm not sure what you plan to do with such a classification, but it is not clearly defined.