I need to find the first occurrence of the attribute ICount and its value must be returned. Only the number from the attribute. How to get it?
< FCount = "1" ICount = "0" Ccount= "1"> I need to find the first occurrence of the attribute ICount and its value must be returned. Only the number from the attribute. How to get it?
< FCount = "1" ICount = "0" Ccount= "1"> Using grep and sed to a) get the integer (!) number following ICount (with any number of digits) and b) removing the ICount specifier:
grep -o 'ICount = "[0-9]\{1,\}' | sed 's/.*"//' Via awk:
awk -F"ICount *= *\"" '{sub(/".*/,"",$2); print $2}' Example
% <<<'< FCount = "1" ICount="0" Ccount= "1">' | awk -F"ICount *= *\"" '{sub(/".*/,"",$2); print $2}' 0 or also all other attributes
% <<<'< 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 It's better to use alone tool:
sed -n '/.*ICount\s*=\s*"\?/{s///;s/[^0-9].*//;p;q;}' file more classic
sed -n 's/.*ICount\s*=\s*"\?\([0-9]\+\)"\s.*/\1/p;/ICount\s*=/q' file or by grep with PCRE
grep -m1 -Po 'ICount\s*=\s*"?\K[0-9]+' file