findstr, for, or other command to do this?

D

djc

Situation: I have a log file with many fields and lines. Each line has say
a computername field. The same computername will be listed several times
thoughout the file. What if I wanted a list of computer names from the file?
meaning 1 instance of each name returned from a command?

example: A log file has 2 fields. ComputerName and DLLVersion. I want to
construct a command or batch file that will return a unique list of computer
names that do not have a DLLVersion equal to 4.1.002. Just using the find
command gets me close but its not a unique list. The same computer names
will be listed multiple times.

any input is appreciated. Thanks.
 
W

wadester

Situation: I have a log file with many fields and lines. Each line has say
a computername field. The same computername will be listed several times
thoughout the file. What if I wanted a list of computer names from the file?
meaning 1 instance of each name returned from a command?

example: A log file has 2 fields. ComputerName and DLLVersion. I want to
construct a command or batch file that will return a unique list of computer
names that do not have a DLLVersion equal to 4.1.002. Just using the find
command gets me close but its not a unique list. The same computer names
will be listed multiple times.

any input is appreciated. Thanks.

Here's a starting place, at least:

Input file in.txt:
computer1 1.0
computer2 2.0
computer1 1.0
computer2 2.0
computer1 3.0
computer2 3.0

Find all unique computer names not at v3.0:
==========================================
@echo off
setlocal enabledelayedexpansion

del tmp.$$$ >nul 2>&1
for /f "tokens=1" %%I in ('findstr /v "3.0" in.txt ^| sort') do (
echo %%I >> tmp.$$$
)

REM Parse thru the sorted temp file and compare each
REM line to the previous one
set oldline=.
for /f %%l in (tmp.$$$) do (
set line=%%l
if !line! NEQ !oldline! echo !line!
set oldline=!line!
)
==========================================

ws
 
M

Matthias Tacke

djc said:
Situation: I have a log file with many fields and lines. Each line has say
a computername field. The same computername will be listed several times
thoughout the file. What if I wanted a list of computer names from the file?
meaning 1 instance of each name returned from a command?

example: A log file has 2 fields. ComputerName and DLLVersion. I want to
construct a command or batch file that will return a unique list of computer
names that do not have a DLLVersion equal to 4.1.002. Just using the find
command gets me close but its not a unique list. The same computer names
will be listed multiple times.

any input is appreciated. Thanks.
You may sort the file and chain some find/fndstr.
Give a more detailed example.
 
J

Jerold Schulman

Situation: I have a log file with many fields and lines. Each line has say
a computername field. The same computername will be listed several times
thoughout the file. What if I wanted a list of computer names from the file?
meaning 1 instance of each name returned from a command?

example: A log file has 2 fields. ComputerName and DLLVersion. I want to
construct a command or batch file that will return a unique list of computer
names that do not have a DLLVersion equal to 4.1.002. Just using the find
command gets me close but its not a unique list. The same computer names
will be listed multiple times.

any input is appreciated. Thanks.

@echo off
setlocal
del /q "%TEMP%\*.DLLVersion
for /f "Tokens=1*" %%a in ('type "YourFile.log"^|findstr /v /c:"4.1.002"') do (
if not exist "%TEMP%\%%a.DLLVersion" @echo %%b>"%TEMP%\%%a.DLLVersion"
)
for /f "Tokens=1* Delims=." %%a in ('dir /b "%TEMP%\*.DLLVersion"') do (
@echo %%a
)
endlocal
exit /b 0



Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
D

djc

cool! I'll play with that I'm sure!

Jerold Schulman said:
@echo off
setlocal
del /q "%TEMP%\*.DLLVersion
for /f "Tokens=1*" %%a in ('type "YourFile.log"^|findstr /v /c:"4.1.002"') do (
if not exist "%TEMP%\%%a.DLLVersion" @echo %%b>"%TEMP%\%%a.DLLVersion"
)
for /f "Tokens=1* Delims=." %%a in ('dir /b "%TEMP%\*.DLLVersion"') do (
@echo %%a
)
endlocal
exit /b 0



Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
D

djc

interesting. Thanks for the reply!

wadester said:
Here's a starting place, at least:

Input file in.txt:
computer1 1.0
computer2 2.0
computer1 1.0
computer2 2.0
computer1 3.0
computer2 3.0

Find all unique computer names not at v3.0:
==========================================
@echo off
setlocal enabledelayedexpansion

del tmp.$$$ >nul 2>&1
for /f "tokens=1" %%I in ('findstr /v "3.0" in.txt ^| sort') do (
echo %%I >> tmp.$$$
)

REM Parse thru the sorted temp file and compare each
REM line to the previous one
set oldline=.
for /f %%l in (tmp.$$$) do (
set line=%%l
if !line! NEQ !oldline! echo !line!
set oldline=!line!
)
==========================================

ws
 
M

Matthias Tacke

Matthias Tacke said:
You may sort the file and chain some find/fndstr.
Give a more detailed example.
Having reviewed the solutions from wadester and Jerold, I think they
don't work the way djc wants.
My try builds an exclude list from computers with matching dll's and in
a second run elminiates the remaining doubles.

@wadester: findstr uses regexp as default. So the dot in "3.0" will
match any char.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
:: Build an exclude list from matching results
del tmp.$$$ >nul 2>&1
for /f "tokens=1" %%A in ('findstr /C:"4.1.002" your.log') do (
echo %%A >> tmp.$$$
)
set oldline=.
for /f "tokens=1" %%B in ('findstr /V /G:"tmp.$$$" your.log^|sort') do (
call :ChkUnique "%%B"
if errorlevel 1 echo %%B
)
goto :eof
:ChkUnique
set line=%1
if %line% EQU %oldline% exit /b 0
set oldline=%line%
exit /b 1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

I didn't take care of the same computername more than once in the
exclude list. Since the sub is already there, here it is implemented:

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
:: Build an exclude list from matching results
del tmp.$$$ >nul 2>&1
set oldline=.
for /f "tokens=1" %%A in ('findstr /C:"4.1.002" your.log^|sort') do (
call :ChkUnique "%%B"
if errorlevel 1 echo %%A >> tmp.$$$
)
set oldline=.
for /f "tokens=1" %%B in ('findstr /V /G:"tmp.$$$" your.log^|sort') do (
call :ChkUnique "%%B"
if errorlevel 1 echo %%B
)
goto :eof
:ChkUnique
set line=%1
if %line% EQU %oldline% exit /b 0
set oldline=%line%
exit /b 1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

:

Hmm, I really hate that, 4 postings in a row.
for /f "tokens=1" %%A in ('findstr /C:"4.1.002" your.log^|sort') do (
call :ChkUnique "%%B"
--------------------^ that should have been "%%A" of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top