我有一个正则表达式,它在这里按预期工作:https : //regex101.com/r/fR4mI1/1
它找到所有1到9的整数,但不是零,或更准确地说,发现不是非数字也不是0。这恰好是我在下面所做的事情,但它不是这样解决的。我的输入中有0,以奇怪的方式显示在我的输出中。我想做的所有其他事情都工作正常,但我根本不需要任何零。我究竟做错了什么?
#!/bin/bash
#not non-digits or 0, replace with capture and new line
#due to confusion, yes I've tried [1-9] same result
#I understand the statement is not intuitive, that's why I clarified in my explanation
nums=$(echo 821312010476 | sed -r 's/([^\D0])/\1\n/g')
declare -a array
#make array
#error
# 01 and 04 from string above return as 01 and 04 and not 1 or 4
#why are there 0's still?
for num in $nums; do
echo $num
array+=($num)
done
for ((index= 0; index <=${#array[@]}; index++)); do
#make string of 2 integer chars to see if value is within range
myTempNum="${array[index]}${array[index+1]}"
if [[ myTempNum -gt 0 && myTempNum -lt 30 ]]
then
echo $myTempNum
index=$index+1
elif [[ ${array[index]} -gt 0 ]]
then
echo ${array[index]}
fi
done
示例输出
8
2
1
3
1
2
01
04
7
6
8
21
3
12
01
04
7
6
您可以同时在此处看到01和04
nums=$(echo 821312010476 | sed -r 's/([^\D0])/\1\n/g; s/0/''/g')
这就是解决方案,sed不能替代零。谢谢anubhava
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句