You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bash中提取指定区间内的日期值?

Extract Date Range from Bash Array

Hey there! Let's work through this problem together. You have a Bash array of dates (with that tricky "24" entry) and want to pull out only the dates between 20170414 and 20170517 (inclusive), keeping the original "24" string in the result. Here's a straightforward way to do it:

Step-by-Step Solution

First, let's confirm we're working with your array correctly—you already converted the space-separated list to an array with data_list=($data_list), which is a great start. Now we'll:

  • Define our start and end date boundaries
  • Loop through each date in the array
  • Handle the special "24" entry by treating it as 20170424 for comparison (since it falls between 20170423 and 20170425 in your list)
  • Check if each date falls within our range
  • Collect matching dates into a new variable

Code Implementation

# Your original date array
data_list=("20170410" "20170411" "20170412" "20170414" "20170415" "20170416" "20170417" "20170419" "20170420" "20170422" "20170423" "24" "20170425" "20170516" "20170517" "20170518" "20170519" "20170520")

# Define our date range boundaries
start_date="20170414"
end_date="20170517"

# Initialize an empty array to hold matching dates (more reliable than string concatenation)
matching_dates=()

# Loop through each date in the original array
for date in "${data_list[@]}"; do
    # Handle the "24" special case: use 20170424 for comparison
    compare_date="$date"
    if [[ "$date" == "24" ]]; then
        compare_date="20170424"
    fi

    # Check if the date is within our range (numerical comparison works here because YYYYMMDD format is ordered)
    if [[ "$compare_date" -ge "$start_date" && "$compare_date" -le "$end_date" ]]; then
        matching_dates+=("$date")
    fi
done

# Convert the matching dates array to a space-separated string
new_variable="${matching_dates[*]}"

# Print the result to verify
echo "$new_variable"

Key Details Explained

  • Using Arrays for Collection: We use an array (matching_dates) to store results instead of building a string directly—this avoids issues with trailing spaces and makes the code cleaner.
  • Special Case Handling: The "24" entry is treated as 20170424 for comparison so it's correctly identified as falling within your desired range, while keeping the original "24" string in the output.
  • Numerical Comparison: Since dates are in YYYYMMDD format, numerical comparison (with -ge for "greater than or equal" and -le for "less than or equal") works perfectly—larger numbers correspond to later dates.
  • Clean Conversion to String: ${matching_dates[*]} joins the array elements into a single space-separated string, just like your expected output.

When you run this code, you'll get exactly the output you want:

20170414 20170415 20170416 20170417 20170419 20170420 20170422 20170423 24 20170425 20170516 20170517

内容的提问来源于stack exchange,提问作者Henry Navarro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:09:03