gpt4 book ai didi

ubuntu - Bash 脚本从输出信息中获取/提取特定信息/单词

转载 作者:行者123 更新时间:2023-12-04 18:27:43 26 4
gpt4 key购买 nike

命令nvcc --version输出以下内容:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_21:14:42_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
我正在尝试获取 cuda 版本,在这种情况下为 10.2从第 4 行开始。
因此我尝试了以下方法:
cudaVersion="$(nvcc --version| grep 'Cuda compilation ')"
echo "$cudaVersion"
然而,这给了我整行作为输出。但是我只想将版本分配给 cudaVersion我相信这必须与 cut 一起使用,但是我无法得到它
提前致谢

最佳答案

使用您显示的示例,您能否尝试以下操作。用 GNU 编写和测试 grep .

nvcc --version | grep -oP '^Cuda compilation.*release \K[0-9]+\.[0-9]+'
解释:为上述添加详细说明。
nvcc --version |  ##Running  nvcc --version command and sending its output to grep command.
grep -oP ##Using grep command using -P option for enabling PCRE ERE here.
'^Cuda compilation.*release \K[0-9]+\.[0-9]+'
##Checking if line starts Cuda compilation till release space.
##\K will skip till matched value(since we don't want in output) dot and digits.

或与任何 awk :
nvcc --version | 
awk '
/^Cuda compilation.*release / && match($0,/release [0-9]+\.[0-9]+/){
print substr($0,RSTART+8,RLENGTH-8)
}
'
解释:为上述添加详细说明。
nvcc --version |      ##Running command nvcc --version sending output to awk here as input.
awk ' ##starting awk program from here.
/^Cuda compilation.*release / && match($0,/release [0-9]+\.[0-9]+/){
##Checking condition if line starts from Cuda compilation till release AND using match function to release space digits dot digits here.
print substr($0,RSTART+8,RLENGTH-8) ##printing sub string with matches value and printing Only version here.
}
'

关于ubuntu - Bash 脚本从输出信息中获取/提取特定信息/单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66670123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com