I have a directory that contains txt files with unique times that gets generated overnight. I am trying to list those files given the unique timestamps in the file. Some example of file names are the following:
file_20210122_1130_1.txt
file_20210122_1133_1.txt
file_20210122_1221_1.txt
file_20210122_1342_1.txt
file_20210122_1721_1.txt
file_20210122_1911_1.txt
file_20210122_2009_1.txt
file_20210122_2020_1.txt
file_20210122_2130_1.txt
...
I want the ability to list the files (using pattern contained in the filename itself) between 1900 and 2100 and it should list the following:
file_20210122_1911_1.txt
file_20210122_2009_1.txt
file_20210122_2020_1.txt
I am trying to use the below code snippet but it partially works and does NOT list all of the files.
cd /home/somedir/files;
ls *.txt | awk '/1900/,/2100/'
Any help would be appreciated.
Thank you all for your responses. I used answers from @the-fourth-bird and @dawg and tweaked it to make it work for me.
I used the following:
printf "%s\n" file_*.txt
to list all of the files and then the following:
awk -v a="$start" -v b="$stop" 'BEGIN {FS="_"}; {if($3 >= a && $3 <= b) print}'
to get files between 1900 (start) and 2100 (stop)
The final command is:
printf "%s\n" file_*.txt | awk -v a="$start" -v b="$stop" 'BEGIN {FS="_"}; {if($3 >= a && $3 <= b) print}';
It gives the output of (which I wanted):
file_20210122_1911_1.txt
file_20210122_2009_1.txt
file_20210122_2020_1.txt
Thank you for all your help!