Picking the latest file based on a timestamp for a Dynamic file name

Hi ,

I did the initial search but could not find what I was expecting for.

15606Always_9999999997_20160418.xml
15606Always_9999999998_20160418.xml
15606Always_9999999999_20160418.xml
9819Always_99999999900_20160418.xml
9819Always_99999999911_20160418.xml

Above is the list of files I receive.

The file name is started with a key value and its changes.

I need to pick the latest file based on the seconds (_99999999911_) in the file name for each key value.

The result would be

15606Always_9999999999_20160418.xml
9819Always_99999999911_20160418.xml

I have script to select if the file name pattern does not change .. But in my case the key value is present in the starting of the file name.

Any help would be appreciated.

Thank you

Like so:

for FN in $(ls -r *.xml); do KEY=${FN%%[A-Z]*}; [ "$KEY" = "$OLDKEY" ] || echo $FN; OLDKEY=$KEY; done
9819Always_99999999911_20160418.xml
15606Always_9999999999_20160418.xml

?

1 Like

Thanks a lot .. This loop worked after I initialized the OLDKEY variable.

But I am still not clear how the code is working and getting the result. I believe I understood the part where you are storing the key value in to $KEY on a -r result .. But not clear how the latest file is picked based on the seconds in the file.

It will be of great help if you can explain how the code is working.

Thank you

The files are sorted in reverse order; that why the "command substitution" for ls needs to be used. So per key, the latest version is the first one. For a new key, the first line is printed as OLDKEY still holds the old key, and key is stored in OLDKEY for further reference.

With awk it looks simpler:

ls -r | awk -F_ '/\.xml$/ && $1!=p1 {print; p1=$1}'
1 Like