N
N
Nikolay Baranenko2020-01-30 20:18:28
linux
Nikolay Baranenko, 2020-01-30 20:18:28

How to properly cut text in bash?

Hello.

In general, I need to pull out the path to site-packages

#  /opt/anaconda/envs/py36/bin/python -m site
sys.path = [
    '/etc/systemd/system',
    '/opt/anaconda/envs/py36/lib/python36.zip',
    '/opt/anaconda/envs/py36/lib/python3.6',
    '/opt/anaconda/envs/py36/lib/python3.6/lib-dynload',
    '/opt/anaconda/envs/py36/lib/python3.6/site-packages',
    '/opt/anaconda2/envs/py36/lib/python3.6/site-packages',
]
USER_BASE: '/root/.local' (exists)
USER_SITE: '/root/.local/lib/python3.6/site-packages' (doesn't exist)
ENABLE_USER_SITE: True


googled this way

/opt/anaconda/envs/py36/bin/python -c "import site; print(site.getsitepackages())"
['/opt/anaconda/envs/py36/lib/python3.6/site-packages']


great, but then I need to trim [' and ']

to admit bash never solved this problem,
as a result, I got this script

pathp="/opt/anaconda/envs/py36/bin/python -c \"import site; print(site.getsitepackages())\""
path1=$(eval "$pathp")
path1=$(echo $path1 | cut -c3-)
path1=$(echo $path1 | rev | cut -c3- | rev)


total

echo $path1
/opt/anaconda/envs/py36/lib/python3.6/site-packages


I suspect there is some more beautiful way, which one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-01-30
@drno-reg

Specifically in your case:

python -c "exec(\"import site\nfor im in site.getsitepackages():print(im)\")"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question