我有一个字符串变量,可能有以下值的组合-
v="2020-01-2020-04,2020-11"我想把上面的值转换成如下的数组-
array=(2020-01,2020-02,2020-03,2020-04,2020-11)请任何人帮助我如何实现这一点,解释范围部分,并提取数组中相应的数据?
注意,字符串中的值是YYYY格式的Monthdate。
我试过用下面的代码来根据",“来分割,但是无法处理范围-
IN="2020-01-2020-04,2020-11"
arrIN=(${IN//,/ })
echo ${arrIN[1]} 发布于 2022-01-21 12:36:43
生成范围并将日期存储在bash数组中:
next_month() {
local y m
IFS='-' read y m <<< "$1"
if [ "$m" == 12 ]
then
m=1 y=$(( 10#$y + 1 ))
else
m=$(( 10#$m + 1 ))
fi
printf '%04d-%02d\n' "$y" "$m"
}对于GNU date来说,这是微不足道的。
next_month() { date -d "$1-01 +1month" '+%Y-%m'; }甚至是BSD date
next_month() { date -j -v '+1m' -f '%Y-%m' "$1" '+%Y-%m'; }然后,
v="2020-01-2020-04,2020-11"
array=()
for date in ${v//,/ }
do
[[ $date =~ ^([0-9]{4}-(0[1-9]|1[0-2]))(-([0-9]{4}-(0[1-9]|1[0-2])))?$ ]] || continue
inidate=${BASH_REMATCH[1]}
enddate=${BASH_REMATCH[4]:-$inidate}
until [ "$inidate" == "$enddate" ]
do
array+=( "$inidate" )
inidate=$(next_month "$inidate")
done
array+=( "$inidate" )
donedeclare -p array
# declare -a array='([0]="2020-01" [1]="2020-02" [2]="2020-03" [3]="2020-04" [4]="2020-11")'发布于 2022-01-21 13:19:41
在bash中,请您尝试以下几种方法:
#!/bin/bash
v="2020-01-2020-04,2020-11"
IFS=, read -ra a <<< "$v" # split $v on comma(s) into an array a
for i in "${a[@]}"; do
if [[ $i =~ ^[0-9]{4}-[0-9]{2}$ ]]; then
array+=("$i") # single YYYY-MM
elif [[ $i =~ ^([0-9]{4})-([0-9]{2})-([0-9]{4})-([0-9]{2})$ ]]; then
# range of two YYYY-MM's
if (( 10#${BASH_REMATCH[1]} != 10#${BASH_REMATCH[3]} )); then
echo "the range of year not supported."
exit 1
else
for (( j = 10#${BASH_REMATCH[2]}; j <= ${BASH_REMATCH[4]}; j++ )); do
# expand the range of months
array+=( "$(printf "%04d-%02d" $((10#${BASH_REMATCH[1]})) "$j")" )
done
fi
fi
done
(IFS=","; echo "${array[*]}") # print the result输出:
2020-01,2020-02,2020-03,2020-04,2020-11https://stackoverflow.com/questions/70801221
复制相似问题