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
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