How to Combine Two Different tokens, delims, and findstrs?

74 Views Asked by At

I have an output text file for a software license usage which displays a ton of information and I want to extract pieces of each line depending on what that line contains to simplify the output.

Here is the Codes that I have, which work perfectly, except I need to combine them so if a line in the text file contains "maxReleaseNumber" it does one thing, but if the line contains "internal Id" it does a different thing.

@ECHO OFF
SET InFile=C:\temp\ListUsers.txt



cd C:\Program Files\SoftwareSystemes\License Server\
LicSrv -admin -run "c XXlic4 1004; glu -feature XXX-FTXHDXMCE>%InFile%; glu -feature XXX-AMEMCE>>%InFile%; glu -feature XXX-MCE>>%InFile%; glu -feature XXX-HDXMCE>>%InFile%; glu -feature X3X>>%InFile%;quit" 

echo off


::These 2 lines are the codes I want combined, it only works right now if only one is off at a time.
for /f "tokens=1, 14, 15, 16, 17 delims= " %%a in ('type C:\temp\ListUsers.txt ^|findstr /c:"maxReleaseNumber"') do (echo %%a %%b %%c %%d %%e)

::for /f "tokens=13, 14, 15, 16, 17, 18, 21, 22, 24 delims=. " %%a in ('type C:\temp\ListUsers.txt ^|findstr /c:"internal Id"') do (echo %%g %%h %%i %%a %%b %%c %%d %%e %%f)

del %InFile%

Pause

Edit 1 I apologize, it is a rather detailed start text file and contains a lot of sensitive data. This is my attempt at showing what a dumbed down version would look like. You can see I need a code that runs through each line, and if it finds one string, then it runs one set of tokens on it, but if it finds the other string, it runs a different set of tokens on it.

Input ListUsers.txt

        Windows System XX (1234567896)
  XXX-ABC maxReleaseNumber: 0  maXRDt: 2/14/24, 5:59:02 Count: 1 In Use: 1
    internal Id: [email protected] host: ENG: 02 Granted: 08/18/23
  XXX-DEF maxReleaseNumber: 0  maXRDt: 2/14/24, 5:59:02 Count: 2 In Use: 1
    internal Id: [email protected] host: ENG: 12 Granted: 08/18/23
  XXX-EFG maxReleaseNumber: 0  maXRDt: 2/14/24, 5:59:02 Count: 10 In Use: 3
    internal Id: [email protected] host: SHOP: 02 Granted: 08/18/23
    internal Id: [email protected] host: PM: 02 Granted: 08/16/23
    internal Id: [email protected] host: ENG: 08 Granted: 08/17/23

Desired Output:

XXX-ABC Count: 1 In Use: 1
Josh@gmail host: ENG: 02 Granted: 08/18/23
XXX-DEF Count: 2 In Use: 0
Bill@gmail host: ENG: 12 Granted: 08/18/23
XXX-EFG Count: 10 In Use: 3
Adam@gmail host: SHOP: 02 Granted: 08/18/23
Caleb@gmail host: PM: 02 Granted: 08/16/23
Sam@gmail host: ENG: 08 Granted: 08/17/23
3

There are 3 best solutions below

0
Magoo On

Since you haven't shown us any sample data or told us what exectly you wish to extract from that data, this would be a guess:

for /f "tokens=1, 14, 15, 16, 17 delims= " %%a in ('type C:\temp\ListUsers.txt ^|findstr /c:"maxReleaseNumber"') do (
 echo %%a %%b %%c %%d %%e

 for /f "tokens=13, 14, 15, 16, 17, 18, 21, 22, 24 delims=. " %%A in ('type C:\temp\ListUsers.txt ^|findstr /c:"internal Id"') do (
  echo %%G %%H %%I %%A %%B %%C %%D %%E %%F
  echo ----------
  echo %%G %%H %%I %%A %%B %%C %%D %%E %%F %%a %%b %%c %%d %%e
  echo ----------
 )
)

...since metavariables are case-sensitive.

0
Stephan On

Check your tokens. There are no more than 12 per line, tokens numbers greater than 12 don't work. Aside that, it's a simple matter of "if - then" and counting tokens:

@ECHO OFF
setlocal
SET "InFile=ListUsers.txt"
for /f "tokens=1-13 delims=. " %%a in ('type "%infile%" ^|findstr /c:"maxReleaseNumber" /c:"internal Id:"') do (
  if "%%b" == "maxReleaseNumber:" echo %%a %%g %%h %%i %%j %%k
  if "%%a %%b" == "internal Id:" echo %%c %%i %%j %%k %%l
)

gives exactly the desired output with your sample data.

2
Aacini On

This (much simpler) solution works OK:

@echo off

for /F "tokens=1-7* delims=. " %%a in (ListUsers.txt) do (
   if "%%b" == "maxReleaseNumber:" echo %%a %%g %%h
   if "%%b" == "Id:" echo %%c %%h
)