从文件中提取字符串并将其存储在变量中

Extract a string from a file and store it in a variable

我有一个包含以下行的文件。

ARG VERSION="6.0.0" // Here 6.0.0 is the version and could be any number.

我需要提取此值 6.0.0 并将其存储为 shell 变量,以便稍后在构建中使用。

有办法实现吗?

sed -nE 's/ARG VERSION=\"(.*)\"//p' <file_with_string> 可能有效。

在这种情况下,我们捕获双引号之间的内容,select 首先捕获 group(</code>) 和 print(<code>p)

能否请您尝试以下。

awk 'match([=10=],/ARG VERSION="[^"]*/){print substr([=10=],RSTART+13,RLENGTH-13)}' Input_file

解释:为以上添加详细解释。

awk '                                      ##Starting awk program from here.
match([=11=],/ARG VERSION="[^"]*/){            ##Using match function to match regex ARG VERSION=" till " here.
  print substr([=11=],RSTART+13,RLENGTH-13)    ##Printing sub-string of current line starting point is RSTART ending point is RLENGTH.
}
' Input_file                               ##Mentioning Input_file name here.