Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select date range with awk

I have list of folders with name format as YYYYMMDD and I am trying to get the subset of folders with in a date range using awk command. However it is not including folder name on the right boundary.

List of directories:

bash-4.2# ls -d */
20240111/  20240112/  20240113/  20240114/  20240115/  20240118/  20240120/  dest/  test-files/  test/

Note: The folder may also contain subfolders whose name is not in YYYYMMDD format. For instance ‘test-files/’ as above

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

AWK command to get a range (20240114 – 20240120) of subfolder names:

bash-4.2#  ls -d */ | awk -v start=20240114 -v end=20240120 '{d=substr($0,0); if (d>=start&&d<=end) print}' 
20240114/
20240115/
20240118/

Here, though the if condition has less than equal to condition "d<=end" the output is not including folder with name "20240120/". Not sure why this one is not included in the result.

Also, please note that I don’t have much experience with advanced linux commands. So, please kindly advise if the above command is right and works always. For instance if there is any subfolder with unexpected name, is there a chance that this command returns wrong results.

>Solution :

20240120/ collates after 20240120, your range doesn’t include it. Do it like this instead:

awk -F / -v start=20240114 -v end=20240120 '$1 >= start && $1 <= end'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading