Linux 在程序输出(Bash)中拾取特定值
我在一个C程序调用的linux终端上运行LIBSVM。好的,我需要选择输出,但格式如下Linux 在程序输出(Bash)中拾取特定值,linux,string,bash,sed,Linux,String,Bash,Sed,我在一个C程序调用的linux终端上运行LIBSVM。好的,我需要选择输出,但格式如下 Accuracy = 80% (24/30) (classification) 我只需要选择“80”值作为整数。我尝试了sed,得到了以下命令: sed 's/[^0-9^'%']//g' 'f' >> f 这是过滤输出中的所有整数,因此还不能工作,所以我需要帮助。提前感谢在PCRE模式(-p)下尝试grep,只打印匹配的部分(-o),并使用: regexp: [0-9] # match a
Accuracy = 80% (24/30) (classification)
我只需要选择“80”值作为整数。我尝试了sed,得到了以下命令:
sed 's/[^0-9^'%']//g' 'f' >> f
这是过滤输出中的所有整数,因此还不能工作,所以我需要帮助。提前感谢在PCRE模式(
-p
)下尝试grep
,只打印匹配的部分(-o
),并使用:
regexp:
[0-9] # match a digit
+ # one or more times
(?=%) # assert that the digits are followed by a %
使用
awk
非常简单。标识所需的列并从中删除“%”符号。/^accurity/
正则表达式确保仅在以accurity开头的行上执行操作。如果文件只包含一行,则不需要它
awk '/^Accuracy/{sub(/%/,"");print $3}' inputFile
或者,您可以将空格和%
设置为字段分隔符并执行以下操作
awk -F'[ %]' '/^Accuracy/{print $3}' inputFile
如果要使用sed
执行此操作,可以尝试以下操作:
sed '/^Accuracy/s/.* \(.*\)%.*/\1/' inputFile
这可能适用于您(GNU-sed):
sed '/^Accuracy/s/.* \(.*\)%.*/\1/' inputFile
sed -nr '/^Accuracy = ([^%]*)%.*/s//\1/p' file