Extract file names from file and set variable to 'highest' one

HI Folks -

I have a requirement where I need to scan a text file for a list of files.

The file, we'll called it, files.txt looks like such:

inbox/EBS/Client_GL_Detail_PBCS_112517_SEP2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_112617_SEP2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_120817_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_DEC2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121017_OCT2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_DEC2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_NOV2017.txt
 inbox/EBS/Client_GL_Detail_PBCS_121117_OCT2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.txt
 inbox/Client/Client_GL_Detail_PBCS_111417_SEP2017.xls
 outbox/Client_2678.dat
 outbox/Client_2682.dat
 outbox/logs/Client_2954.log
 outbox/logs/Client_2955.log
 outbox/logs/Client_2956.log
 outbox/logs/Client_2957.log
 outbox/logs/Client_2958.log
 outbox/logs/Client_2959.log
 outbox/logs/Client_2960.log
 outbox/logs/Client_2961.log
 outbox/logs/Client_2962.log
 outbox/logs/Client_2963.log
 outbox/logs/Client_2964.log
 
 
 Total 489

I need to scan the file, searching for this string:

outbox/logs/Client_

Once found, I then need to understand if that string is suffixed with the highest numerical value on the end and then set that name (less the path) to a variable.
There will always be 4 digits and the 'highest' one is always the latest, which I need to extract.

I have done this on batch with ease but struggling here. I am using bash.

Thank you.

Why can't you use the batch script? What is that script, and what is not working with it?

Well, its Windows, not *Nix.

Client moved to exa server from Windows.

Like so?

VAR=$(sort -r files.txt | sed -n '\#outbox/logs/Client_# {s#.*/##; p; q}'
echo $VAR
Client_2964.log

Thank you Rudi!

I'm getting an error saying:

Unexpected EOF will looking for matching ')'

Yes. I missed the trailing ) when copying the line. What were your (re)actions when encountering this error message?

Hi Rudi -

Yes - I'm trying to understand where to place that but can't seem to make it work.

What does this message tell you?

Is there an ( somewhere in that code that doesn't have the matching ) that the shell is telling you it can't find?

With over 150 posts in this forum, you been given lots of code that uses command substitutions. Do you really intend to tell us that you can't, with a little bit of trial and error, figure out where the end of that command substitution should be?

Please show us (in CODE tags) what you have tried to solve this problem on your own!

And, please, be very careful when copying diagnostic messages produced by the shell. Trying to search through code to find out what produced an error message when the text you have shown us is not what was actually written by the shell just causes confusion. Did your shell really say:

Unexpected EOF will looking for matching ')'

???

Wow - I feel silly!

I was trying to operate on my phone, but clearly that was a mistake. I've added the closing paren and everything is working as expected.

As always, thank you both!

Would a simple ls not be neater?

VAR=$(ls outbox/logs/Client_* | tail -1)                 # Get the relative-path filename of the last file
VAR="${VAR##*/}"                                         # Trim everything up to the last /
echo $VAR

Maybe I could find a way to get the last entry without a tail (which generates an extra process) then everything apart from the ls to read the directory would be internal to the shell process and probably quick. The sed is a fairly heavy process given that it has so much functionality (that you do not need here)

If this is a once-per-run selection though, this might not matter. It is a choice of coding style which way you choose to go.

Of course, this does not take account of zero matching files and you have to be certain of your current directory given that this is searching a relative path.

Kind regards,
Robin