Page 1 of 1

Get line with highest numerical string

Posted: 12 Mar 2020 18:50
by SIMMS7400
Hello Folks -

I have a output file that I need to parse. The output file consists of this:
outbox/logs/DIM_BPMC_PLN - Account_292.log
outbox/logs/DIM_BPMC_PLN - Account_287.log
outbox/logs/DIM_BPMC_PLN - Account_285.log
outbox/logs/DIM_BPMC_PLN - Account_284.log
outbox/logs/Account Reconciliation Manager_481.log
outbox/logs/Account_Reconciliation_Manager_481.log
outbox/logs/DIM_REGEN - Custom_2080.log
outbox/logs/DIM_REGEN - Custom_2079.log
outbox/logs/DIM_BPMC_PLN - Account_207.log
outbox/logs/DIM_BPMC_PLN - Account_206.log
Test_test_test.txt
Test test test.txt
Test1_2_3 4.txt
Testing_808.txt
Test55.txt
Test5_600.txt
Test_Test.zip
Test2.zip
Test 4.zip
Test10 11_12.zip
What I need to do is return the line in the file that start with 'outbox/logs/' that has the highest number suffixed to the name. FOr instance, the code should return the following line:
outbox/logs/DIM_REGEN - Custom_2080.log
This can be done very easily in powershell but wondering if it's easy enough in batch? Thanks!

Re: Get line with highest numerical string

Posted: 12 Mar 2020 20:07
by Hackoo
With your example that you provide, this batch script may be did the trick :idea:

Code: Select all

@echo off
Title Get line with highest numerical string
Set "InputFile=%1"
If "%~x1" NEQ ".txt" Goto :Help
Set "OutPutFile=%~dp0OutPutFile.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"
@for /f "tokens=* delims=" %%a in ('Type %InputFile% ^|find /I "outbox" ^|sort') do set "LastLine=%%a"
If defined LastLine (
	Color 0A & Mode 80,3 & echo(
	echo %LastLine%>%OutPutFile%
	echo 		%LastLine%
	Timeout /T 2 /nobreak>nul
	Start "" "%OutPutFile%" & Exit
) Else (
	Color 0C & Mode 80,3 & echo( 
	echo		Nothing to parse into this file "%InputFile%"
	Timeout /T 3 /nobreak>nul & Exit
)
::------------------------------------------------------------------
:Help
Color 0C & Mode 80,3
echo(
echo       Usage : Drag and Drop a txt file over this script:"%~nx0"  
Timeout /T 5 /nobreak>nul & Exit
::------------------------------------------------------------------

Re: Get line with highest numerical string

Posted: 13 Mar 2020 01:49
by SIMMS7400
Thank you Hackadoo.

Unfortunately, I don't think it's fail proof. if I add this line to the file:
outbox/logs/DIM_BPMC_PLN - Account_2082.log
It doesn't return it. Furthermore, if I add "7898574" to a file name, it doesn't pull that either.
outbox/logs/DIM_BPMC_PLN - Account_7898574.log

Re: Get line with highest numerical string

Posted: 13 Mar 2020 02:00
by Aacini
This works:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set lastNum=0

for /F "delims=" %%a in ('findstr "outbox/logs/" test.txt') do (
   set "a=%%~Na"
   for %%b in ("!a:_=.!") do set "num=%%~Xb"
   if !num:~1! gtr !lastNum! set "lastNum=!num:~1!" & set "lastLine=%%a"
)

echo %lastLine%

Re: Get line with highest numerical string

Posted: 13 Mar 2020 19:08
by dbenham
It wouldn't be hard to write a custom JScript or VBScript solution.
I went ahead and developed a JREPL.BAT solution - it is significantly faster than any pure batch solution once the input becomes "large".
I used line continuation just to make it easier to read.

Code: Select all

jrepl "^outbox/logs/.*?(\d+)\.log$"^
        "$txt=false;num=parseInt($1);if (num>max){max=num;str=$src}"^
        /jbeg "var max=0,str='',num" /jend "stdout.writeLine(str)" /jmatchq /f test.txt

Dave Benham

Re: Get line with highest numerical string

Posted: 14 Mar 2020 19:15
by siberia-man
Also (in addition to dbeham's suggestion) the wsx tool can help you to get the required result:

Code: Select all

wsx /n /e:"m=LINE.match(/^outbox\/logs\/.*?(\d+)\.log$/)||next();n=Number(m[1]);if(n>max){max=n;str=LINE}" /begin:"str='';max=0" /end:"echo(str)" test.txt

Code: Select all

wsx /n /e:"m=LINE.match(/^outbox\/logs\/.*?(\d+)\.log$/)||next();n=Number(m[1]);if(n>max){max=n;str=LINE}" /let:str= /let:max=0 /end:"echo(str)" test.txt

Re: Get line with highest numerical string

Posted: 22 Mar 2020 03:11
by SIMMS7400
Thank you, all!!

I reallly Aacini's and Dave's solutions and I am going to leverage both. The file to scan will only be about 200 lines max, give or take a few.

Thank you again and I hope everyone is healthy!