Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在程序输出(Bash)中拾取特定值_Linux_String_Bash_Sed - Fatal编程技术网

Linux 在程序输出(Bash)中拾取特定值

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

我在一个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 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