如何隔离一行文本的一部分以便将其用作变量
How to isolate part of a line of text in order to use it as a variable
我正在创建 bash 脚本,想从文件中的一行检查程序的版本号,并用它来进行不同的检查和操作。
文件中的版本行如下所示(示例):
Program Version 1.3
或
Program Version 1.3.1
它在不同版本的不同行上,但始终遵循相同的语法。如何删除第一部分并仅隔离版本号以便将其放入变量中?
我猜你得到了 Program Version 1.3.1
例如启动某种命令。好吧,试试这个:
#!/bin/bash
version=$(yourCommandWhichShowsVersion 2> /dev/null | egrep "^Program Version [0-9]" | awk '{print }')
解释:
- 您需要启动显示版本的命令
- 您将输出重定向到 egrep,它搜索仅匹配以
^
开头的所有行 <- 这是开始字符串,包含所需的文本 Program Version
,而此 [0-9]
是匹配一个数字。如果您不知道版本可以是 1.3 还是 1.3.1 或 1,这就是您所需要的。
- awk 将转到 "select" 第二列(第一列是 "Program version",第二列是版本号)
祝你好运!
Value=$(cat program.txt | grep Program\ Version\ | sed "s/Program \Version\ //g")
只需找到程序版本,然后用 sed 将其删除。
编辑:抱歉看错了。删除的版本号
使用 GNU grep
和 -P
进行 perl 风格的 regEx
匹配,并且 -o
标记 return 仅匹配模式。
grep -oP 'Program Version \K[^ ]*' file
1.3.1
将其保存在变量中
versionNo="$(grep -oP 'Program Version \K[^ ]*' file)"
printf "%s\n" "$versionNo"
1.3.1
使用perl
regEx
本身,
perl -lne 'print "" if /^Program Version (\d.+)/' file
1.3.1
在变量中,
versionNo="$(perl -lne 'print "" if /^Program Version (\d.+)/' file)" file
printf "%s\n" "$versionNo"
1.3.1
使用GNU sed
sed -r 's/Program Version ([[:digit:]].+).*//' file
1.3.1
和
versionNo="$(sed -r 's/Program Version ([[:digit:]].+).*//' file)" file
printf "%s\n" "$versionNo"
1.3.1
我会为此使用 grep
和 cut
,因为模式是固定的:
# find the first occurrence of the line (-m 1) starting with the pattern (^) and
# the third field (cut -f3) is the version
# this makes sure we ignore
# a) multiple occurrences of the pattern, if any
# b) occurrence of "Program Version" anywhere else on a line
# we make no assumption about the format of the version number
version=$(grep -m 1 "^Program Version " program.txt | cut -f3 -d' ')
if [[ -z $version ]]; then
# version not specified
# your exception handler here
fi
问题的一些可能解决方案:
awk awk '/Program Version/{print }' file.txt
grepgrep -oP "Program Version \K[^ ]*" file.txt
sedsed -n '/Program Version /s///p' file.txt
perl perl -lane 'if (/Program Version/) {print $F[2]}' file.txt
(HTH) 希望对您有所帮助。
这是我最后使用的:
Version=`find . -type f -name "filename" -exec grep -h 'Program Version' {} + | awk -F " " '{print }'`
我使用 find + grep 接收包含我需要的版本的行,并在 awk 中使用 -f 将字段分隔符定义为空白 space 并且我将版本号与其余部分分开结果。谢谢大家的回答。
我正在创建 bash 脚本,想从文件中的一行检查程序的版本号,并用它来进行不同的检查和操作。
文件中的版本行如下所示(示例):
Program Version 1.3
或
Program Version 1.3.1
它在不同版本的不同行上,但始终遵循相同的语法。如何删除第一部分并仅隔离版本号以便将其放入变量中?
我猜你得到了 Program Version 1.3.1
例如启动某种命令。好吧,试试这个:
#!/bin/bash
version=$(yourCommandWhichShowsVersion 2> /dev/null | egrep "^Program Version [0-9]" | awk '{print }')
解释:
- 您需要启动显示版本的命令
- 您将输出重定向到 egrep,它搜索仅匹配以
^
开头的所有行 <- 这是开始字符串,包含所需的文本Program Version
,而此[0-9]
是匹配一个数字。如果您不知道版本可以是 1.3 还是 1.3.1 或 1,这就是您所需要的。 - awk 将转到 "select" 第二列(第一列是 "Program version",第二列是版本号)
祝你好运!
Value=$(cat program.txt | grep Program\ Version\ | sed "s/Program \Version\ //g")
只需找到程序版本,然后用 sed 将其删除。
编辑:抱歉看错了。删除的版本号
使用 GNU grep
和 -P
进行 perl 风格的 regEx
匹配,并且 -o
标记 return 仅匹配模式。
grep -oP 'Program Version \K[^ ]*' file
1.3.1
将其保存在变量中
versionNo="$(grep -oP 'Program Version \K[^ ]*' file)"
printf "%s\n" "$versionNo"
1.3.1
使用perl
regEx
本身,
perl -lne 'print "" if /^Program Version (\d.+)/' file
1.3.1
在变量中,
versionNo="$(perl -lne 'print "" if /^Program Version (\d.+)/' file)" file
printf "%s\n" "$versionNo"
1.3.1
使用GNU sed
sed -r 's/Program Version ([[:digit:]].+).*//' file
1.3.1
和
versionNo="$(sed -r 's/Program Version ([[:digit:]].+).*//' file)" file
printf "%s\n" "$versionNo"
1.3.1
我会为此使用 grep
和 cut
,因为模式是固定的:
# find the first occurrence of the line (-m 1) starting with the pattern (^) and
# the third field (cut -f3) is the version
# this makes sure we ignore
# a) multiple occurrences of the pattern, if any
# b) occurrence of "Program Version" anywhere else on a line
# we make no assumption about the format of the version number
version=$(grep -m 1 "^Program Version " program.txt | cut -f3 -d' ')
if [[ -z $version ]]; then
# version not specified
# your exception handler here
fi
问题的一些可能解决方案:
awk awk '/Program Version/{print }' file.txt
grepgrep -oP "Program Version \K[^ ]*" file.txt
sedsed -n '/Program Version /s///p' file.txt
perl perl -lane 'if (/Program Version/) {print $F[2]}' file.txt
(HTH) 希望对您有所帮助。
这是我最后使用的:
Version=`find . -type f -name "filename" -exec grep -h 'Program Version' {} + | awk -F " " '{print }'`
我使用 find + grep 接收包含我需要的版本的行,并在 awk 中使用 -f 将字段分隔符定义为空白 space 并且我将版本号与其余部分分开结果。谢谢大家的回答。