我需要找到该属性的第一次出现ICount
,并且必须返回它的值。仅属性中的数字。如何获得?
< FCount = "1" ICount = "0" Ccount= "1">
答案1
使用grep
andsed
来 a) 获取后面的整数 (!) ICount
(具有任意位数)和 b) 删除ICount
说明符:
grep -o 'ICount = "[0-9]\{1,\}' | sed 's/.*"//'
答案2
通过awk
:
awk -F"ICount *= *\"" '{sub(/".*/,"",$2); print $2}'
例子
% <<<'< FCount = "1" ICount="0" Ccount= "1">' | awk -F"ICount *= *\"" '{sub(/".*/,"",$2); print $2}'
0
或所有其他属性
% <<<'< FCount = "1" ICount="0" Ccount= "1">' | awk -F"Ccount *= *\"" '{sub(/".*/,"",$2); print $2}'
1
% <<<'< FCount = "1" ICount="0" Ccount= "1">' | awk -F"FCount *= *\"" '{sub(/".*/,"",$2); print $2}'
1
答案3
最好单独使用工具:
sed -n '/.*ICount\s*=\s*"\?/{s///;s/[^0-9].*//;p;q;}' file
更经典
sed -n 's/.*ICount\s*=\s*"\?\([0-9]\+\)"\s.*/\1/p;/ICount\s*=/q' file
或通过 grep 和 PCRE
grep -m1 -Po 'ICount\s*=\s*"?\K[0-9]+' file