Page 1 of 1

How to sort file contents based on pattern?

Posted: 09 Apr 2019 01:38
by SIMMS7400
Hi Folks -

I have a text file that looks like such:


outbox/logs/PLAN - Custom_8001.log
outbox/logs/PLAN - Custom_242.log
outbox/logs/PLAN - Custom_243.log
outbox/logs/PLAN - Custom_244.log
outbox/logs/PLAN - Custom_245.log
outbox/logs/PLAN - Custom_246.log
outbox/logs/PLAN - Custom_40.log
outbox/logs/PLAN - Custom_41.log
outbox/logs/PLAN - Custom_42.log
outbox/logs/PLAN - Custom_43.log
outbox/logs/PLAN - Custom_44.log
outbox/logs/PLAN - Custom_45.log
outbox/logs/PLAN - Custom_46.log
As you can see, the string after the "_" varies from 2 to 4 digits. I ALWAYS need to get the file names with the "highest" numerical value after the "_".

This text file is written to by an application and it writes in in descending order, and since the batch will start at the top go down, its opposite of what I need.

Here is a batch script that works if there is no variation in length of numbers after the "_" :

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

CD C:\TEMP

SET "SEARCH=outbox/logs/PLAN - Custom_"
SET "FILE=temp.txt"

SORT "%FILE%"

FOR /F "tokens=*" %%A IN ('FINDSTR /c:"%SEARCH%" %FILE%') DO (
  SET "NAME=%%~nA" & IF "!NAME!" GTR "!MAXFILE!" SET "MAXFILE=!NAME!.log"
)
But obviously rhats not dynamic and I can't always gaurentee that will always be the case so I need to force it to grab the highest. Is this easy to achieve?

Thanks!

Re: How to sort file contents based on pattern?

Posted: 09 Apr 2019 05:32
by penpen
SIMMS7400 wrote:
09 Apr 2019 01:38
But obviously rhats not dynamic and I can't always gaurentee that will always be the case so I need to force it to grab the highest. Is this easy to achieve?
Yes, just cut out the number in the filename using "tokens=2 delims=_." and then compare the numbers with the content of an environment variable (for example named VALUE) initiated to -1-.


penpen

Re: How to sort file contents based on pattern?

Posted: 09 Apr 2019 06:58
by SIMMS7400
Ah yes, very simple using the tokens.

Code: Select all

FOR /F "tokens=2 delims=_." %%A IN ('FINDSTR /c:"%OUTBOX_BIN%%NAME%_" %FILE%') DO (
    SET "NUM=%%~A"
    IF !NUM! GTR !MAXNUM! SET "MAXNUM=!NUM!" & SET "MAXFILE=%NAME%_!MAXNUM!.log"
)
THanks!

Re: How to sort file contents based on pattern?

Posted: 09 Apr 2019 07:46
by dbenham
Sorting numerically is one of the prime reasons I wrote JSORT.BAT v4.2 - Case sensitive sort with option for numeric sort. It is another hybrid JScript/batch utility. Full documentation is available via JSORT /?

I believe you are looking for the highest numbered file, so the following options are needed:
  • /N - Treat consecutives digits numerically
  • /R - Reverse sort (descending)
  • /C 1 - Keep the first sorted (highest) line only

Code: Select all

call jsort file.txt /n /r /c 1
You can redirect the output, or use the /O option to capture the output in a file.

If you want the value in a variable, then FOR /F works well

Code: Select all

for /f "delims=" %%F in ('jsort file.txt /n /r /c 1') do set "highest=%%F"

Dave Benham

Re: How to sort file contents based on pattern?

Posted: 09 Apr 2019 08:34
by Squashman
SIMMS7400 wrote:
09 Apr 2019 06:58
Ah yes, very simple using the tokens.
Yes as you have been shown and used several times on this forum.
SIMMS7400 wrote:
09 Apr 2019 06:58

Code: Select all

FOR /F "tokens=2 delims=_." %%A IN ('FINDSTR /c:"%OUTBOX_BIN%%NAME%_" %FILE%') DO (
    SET "NUM=%%~A"
    IF !NUM! GTR !MAXNUM! SET "MAXNUM=!NUM!" & SET "MAXFILE=%NAME%_!MAXNUM!.log"
)
No need to set the FOR variable to an environmental variable. Just use the FOR variable directly with your comparison instead.

Code: Select all

set "maxnum=0"
FOR /F "tokens=2 delims=_." %%A IN ('FINDSTR /c:"%OUTBOX_BIN%%NAME%_" %FILE%') DO (
    IF %%A GTR !MAXNUM! (SET "MAXNUM=%%A" & SET "MAXFILE=%NAME%_!MAXNUM!.log")
)