0

I have a list of files that contain a Julian calendar date in them. Examples: XXX_YY_AB21123.TXT, XXX_YY_AB21124.TXT etc. I have hundreds of them for the year. I need a way to search for the files based on the names and only return the ones for a specific range.

Example: Return all filenames that have the Julian date embedded in the name between 60 and 90 (Mar 2021) XXX_YY_AB21060 thru XXX_YY_AB21090

Any ideas?

2

If you need to get the Julian date for a particular day, you can use date's %j format and if your date is the GNU implementation or compatible, use -d to convert it from another format:

$ date -d "2021/03/01" +%j
060

Once you know that, you can use globs and brace expansion to achieve what you want:

$ shopt -s nullglob  # prevent unmatched globs from returning verbatim
$ printf '%s\n' *_*_*{060..090}.TXT
XXX_YY_AB21060.TXT
XXX_YY_AB21061.TXT
XXX_YY_AB21062.TXT
XXX_YY_AB21063.TXT
XXX_YY_AB21064.TXT
XXX_YY_AB21065.TXT
[...]
1
  • Thanks. That did what I needed!
    – JT-66
    Jun 22 at 11:18
1

In zsh:

print -rC1 -- **/*AB21<60-90>.txt(N)

Would print raw on 1 Column the list of filenames that end with AB21 followed by a decimal number in the range 60 to 90 followed by .txt in or below the current working directory (omitting hidden ones).

To compute the range from a given Mar 2021-like representation, you can do:

month='Mar 2021'

zmodload zsh/datetime

# Mar-2021 to epoch (for first day in that month):
TZ=UTC0 strftime -r -s start '%b %Y' $month

# month after in 2021-04 format obtained by adding 35 days:
TZ=UTC0 strftime    -s t     %Y-%m   $(( start + 35 * 86400 ))

# convert that to epoch time (of the first day of month after)
TZ=UTC0 strftime -r -s t     %Y-%m   $t

# convert both to YYJJJ
TZ=UTC0 strftime    -s start %y%j    $start
TZ=UTC0 strftime    -s end   %y%j    $(( t - 86400 ))
range="<$start-$end>"

print -rC1 -- **/*AB$~range.txt(N)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.