I want to extract last part of directories from specific date=2022-01-10 and having construction like :
/rec/flux_entrant/le5/archive/tble91_formation/20220104-020124
/rec/flux_entrant/le5/archive/tble91_formation/20220105-020113
/rec/flux_entrant/le5/archive/tble91_formation/202201013-020124
/rec/flux_entrant/le5/archive/tble91_formation/202201014-020123
...
As you can see we have many date here 20220104 , 20220105 , 202201013 and 202201014
In other world I should develop a loop operator that take all path which exists in :
/rec/flux_entrant/le5/archive/tble91_formation/
and return a list having values like : 202201013-020124 and 202201014-020123 because 202201013 and 202201014 are dates given after 2022-01-10
Any help please ? Thank you
CodePudding user response:
This might be what you're looking for
#!/bin/bash
date='2022-01-10'
for path in /rec/flux_entrant/le5/archive/tble91_formation/*; do
lastpart=${path##*/}
ymd=${lastpart%%-*}
[[ $ymd = ??????0?? ]] && ymd=${ymd:0:6}${ymd:7}
if [[ $ymd = [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then
[[ $ymd > ${date//-} ]] && echo "$lastpart"
else
echo "Unrecognized date format in file $lastpart" >&2
fi
done
CodePudding user response:
Assuming the month and day fields always contain preceding 0,
would you please try:
#!/bin/bash
refdate='2022-01-10'
for f in /rec/flux_entrant/le5/archive/tble91_formation/*; do
base=${f##*/}
if [[ $base =~ ^([0-9]{4})(0[1-9]|01[0-2])(0[1-9]|0[1-3][0-9])- ]]; then
if [[ ${BASH_REMATCH[1]}-${BASH_REMATCH[2]: -2}-${BASH_REMATCH[3]: -2} > $refdate ]]; then
echo "$base"
fi
fi
done
Output:
202201013-020124
202201014-020123
- The regex
^([0-9]{4})(0[1-9]|01[0-2])(0[1-9]|0[1-3][0-9])-will match the formatted date, assigning the bash arrayBASH_REMATCHto the matched year, month, and day. - The expression
${BASH_REMATCH[1]}-${BASH_REMATCH[2]: -2}-${BASH_REMATCH[3]: -2}reformats the array values into the date string asyyyy-mm-dd. - Then the date string is compared with the reference date
2022-01-10.
If the month is between October and December and the string is formatted as 202201001 (1st of October, 2022), it will also work.
CodePudding user response:
A much simpler approach is to use awk. Simply split the last field of each record on '-' and compare against 20220110:
$ ls -1 /rec/flux_entrant/le5/archive/tble91_formation/* |
awk -F'/' '{split($NF, a, "-"); if (a[1]>20220110) print $NF}'
(note: above is ls -(one) not ls -(el))
You can tailor the search for files further by using find instead of ls. Up to you.
Result:
202201013-020124
202201014-020123
You can further sort or split as needed.
