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

List the files with latest data

I was trying to get a list of files with latest data in a day. The day can be found from the file name.

Input files of type abc_YYYYMMDDHH24MISS.txt

abc_20230101033006.txt  
abc_20230101043006.txt
abc_20230102033006.txt  
abc_20230102043006.txt

expected output 
abc_20230101043006.txt
abc_20230102043006.txt

I have tried this command

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

ls | cut -b 1-12|uniq |ls |sort|tail -1

result :(getting only one file)

abc_20230102043006.txt

>Solution :

Using awk:

$ ls -1
abc_20230101033006.txt
abc_20230101043006.txt
abc_20230102033006.txt
abc_20230102043006.txt
$ printf '%s\n' abc_* |
    awk '{k=substr($0, 0,12);a[k]=$0}END{for (i in a) print a[i]}'

abc_20230101043006.txt
abc_20230102043006.txt
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