Sort output by date, and group by specific key in bash script

I have output which looks like this: (for readability i’ve condensed newlines into spaces, eg after each comma)

##[group]filename.json
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]
##[group]filename2.json
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-01T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-03T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]
##[group]filename3.json
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-01T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-03T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]

and I want to sort them and group them by date
eg

##[group]expiresOn "2024-06-01T00:00:00Z"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-01T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-01T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]
##[group]expiresOn "2024-06-02T00:00:00Z"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-02T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]
##[group]expiresOn "2024-06-03T00:00:00Z"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-03T00:00:00Z", "scope": "scope"
"name": "name", "displayName": "displayName", "description": "description", "Category": "waiver", "expiresOn": "2024-06-03T00:00:00Z", "scope": "scope"
##[command]==========================================================================
##[endgroup]

The ##[ notation is used in Azure pipelines output so ideally should remain.

My code so far is:

  1. check each JSON file in a directory for dates that are less than $expiry where $expiry is the date of the execution of the script + userinput days
  2. clean the data to manipulate, eg eliminate quotes
  3. find the beginning and end of the block where our date is
  4. use sed to strip data from the file as a complete block
  5. use grep -A/B to highlight key names and colour them using GREP_COLORS and append the block to the output.
    Note: this output is not an array
howManyDays=${1} # How many days in advance to check for, read from user input
cutoff=$(date --date "+${howManyDays} days" +%s) # Date in epoch seconds
for file in $(ls path/to/file)
do                 
    while read -r line key expiry
    do 
      # Strip off quotes and , from date found
      stripDate=`echo "$expiry" | tr -d "","`
      age=$(date -d "${stripDate}" +%s)
      # Work out the section of the file we want to report on from the line numbers 
      # Line number containing our expiry date
      lineStrip=`echo "$line" | tr -d ":"`
      # Line number before the expiry date where the record begins
      lineBefore=$(($lineStrip - 4))
      # Line number after the expiry date where the record ends
      lineAfter=$(($lineStrip + 4))
      # Use sed to extract the line numbers for one record, then grep to highlight the fields
      if ((${age} < ${cutoff}));then 
        GREP_COLORS= sed -n "$lineBefore","$lineAfter"p $file | 
        GREP_COLORS='mt=03;31' grep --color=always -A4 -B4 -P $expiry | 
        GREP_COLORS='mt=01;34' grep --color=always -A3 -B4 -P 'expiresOn|name|Category|scope|description' | 
        GREP_COLORS='mt=01;32' grep --color=always -A7 -B5 -P 'displayName'
        echo "##[command]=========================================================================="
        fi
    done < <(grep -n expiresOn path/to/file/$file)
done

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật