在bash下,有办法做到这一点,而无需外部工具(叉子),如sed
,awk
或其他:
i=0;
while read string ;do
((i++))
int=${string#*Int-}
int=(${int//[a-z_-]/ })
exp=${string#*Exp-}
exp=(${exp//[a-z_-]/ })
var=(${string//[a-z_-]/ })
printf "Line #%2d contain: Int: %6s, Exp: %6s in %2d values: <%s>\n" \
$i "$int" "$exp" ${#var[@]} "${var[*]}"
done <<<'
newSetupSL5_snolab_Int-300_Exp-10000_3515
snolab_Int-300_Exp-10000_1185
newSetupSL5_snolab_Int-300_Exp-5000_2522
'
Line # 1 contain: Int: , Exp: in 0 values: <>
Line # 2 contain: Int: 300, Exp: 10000 in 4 values: <5 300 10000 3515>
Line # 3 contain: Int: , Exp: in 0 values: <>
Line # 4 contain: Int: 300, Exp: 10000 in 3 values: <300 10000 1185>
Line # 5 contain: Int: , Exp: in 0 values: <>
Line # 6 contain: Int: 300, Exp: 5000 in 4 values: <5 300 5000 2522>
Line # 7 contain: Int: , Exp: in 0 values: <>
或过滤同时包含Exp- 和Int-的行:
i=0
while read string ;do
if [ "$string" != "${string#*Int-*Exp-}" ];then
((i++))
int=${string#*Int-}
int=(${int//[a-z_-]/ })
exp=${string#*Exp-}
exp=(${exp//[a-z_-]/ })
var=(${string//[a-z_-]/ })
printf "Line #%2d contain: Int: %6s, Exp: %6s in %2d values: <%s>\n" \
$i "$int" "$exp" ${#var[@]} "${var[*]}"
fi
done <<<'
newSetupSL5_snolab_Int-300_Exp-10000_3515
snolab_Int-300_Exp-10000_1185
newSetupSL5_snolab_Int-300_Exp-5000_2522
'
Line # 1 contain: Int: 300, Exp: 10000 in 4 values: <5 300 10000 3515>
Line # 2 contain: Int: 300, Exp: 10000 in 3 values: <300 10000 1185>
Line # 3 contain: Int: 300, Exp: 5000 in 4 values: <5 300 5000 2522>