In Batch how can i find a different line?

September 24, 2010 at 06:22:41
Specs: Windows 7
hi Peoples, i have Problem, i have a File, the content is followed.
COM1:
COM2:
COM3:
COM1:
COM2:
COM3:
COM6:

i want to find the a different COM, it show itself only one time. In example COM6:, and other COMs, like COM1,COM2,COM3 show themself 2 times in File.
And COM6: can be somewhere else, not the last line. like following.

COM1:
COM2:
COM8:

COM1:
COM2:
COM6:
COM8:

how can i do it? thanks.
Actually i want to find the COM nummber of a Device that the recently new connected to the Computer.
i use the mode Command, to show the COM nummber, after Pause i connect the new Device to the Computer. Then Press any key. After that i got a File with the content above.
@echo
mode >> com.txt
pause
mode >> com.txt

setlocal EnableDelayedExpansion

FOR /F "tokens=4 delims= " %%a IN (com.txt) DO (
echo %%a | findstr "COM" > nul & if not errorlevel 1 echo %%a >> OutputFile.txt
)


See More: In Batch how can i find a different line?

Report •

#1
September 24, 2010 at 06:38:01
download gawk for windows then do this

c:\test>  gawk "NF{a[$1]++}END{for(i in a) if (a[i]==1) print i}" file
COM6:

GNU win32 packages | Gawk


Report •

#2
September 24, 2010 at 07:45:52
thank you very much ghostdog,
I know perhaps that there is some better Language to solve the Problem, but i don't want to Installation a new language like gawk. can i just solve the problem only with Batch?

Thank you.


Report •

#3
September 24, 2010 at 08:29:18
you only need to install it once. its just an exe file (gawk.exe) which you can bring anywhere to use.

GNU win32 packages | Gawk


Report •

Related Solutions

#4
September 24, 2010 at 09:15:41
LOL It might get done in batch if I knew what was needed.

I have no idea what this means:

"i want to find the a different COM"


=====================================
Helping others achieve escape felicity

M2


Report •

#5
September 24, 2010 at 09:18:09
Mechanix2Go:
He wants to find the line not in the file twice.

Report •

#6
September 24, 2010 at 10:11:57
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (COMfile) do (
set /a N+=1
set V!N!=%%a
)

for /L %%i in (1 1 !N!) do (
for /f "tokens=* delims= " %%a in ('find /c "!V%%i!" ^< COMfile') do (
  if %%a equ 1 (
  echo.only one !V%%i!
  goto :eof
  )
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#7
September 24, 2010 at 18:46:34
@M2, good effort but too slow for my liking, calling find too many times :)

Also, it doesn't take care of multiple single COM

C:\test>more file
COM1:
COM2:
COM1:
COM2:
COM3:
COM6:

C:\test>test.bat
only one COM3:

COM6 is missing from output


GNU win32 packages | Gawk


Report •

#8
September 25, 2010 at 05:02:00
Hi ghostdog ,

[1] Correct, of course, that multiple FINDs is not the way to go. I think there's a non-FIND, maybe a 'no external', way to do this. But I haven't worked it out yet.

[2] Also correct that it doesn't accout for multi unique lines. I was going by what R2 said. I had now clue from OP what the task was.

We can easily accomodate multi unique lines:

=================================================

@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (COMfile) do (
set /a N+=1
set V!N!=%%a
)

for /L %%i in (1 1 !N!) do (
for /f "tokens=* delims= " %%a in ('find /c "!V%%i!" ^< COMfile') do (
  if %%a equ 1 (
  echo.only one !V%%i!
  )
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#9
September 25, 2010 at 12:32:43
Here's a script using no externals.

=========================================

@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (COMfile) do (
  set /a N+=1
  set V!N!=%%a
)

for /L %%i in (1 1 !N!) do (
  call :sub1 %%i
)

for %%r in (!R!) do (
  set V%%r=
)

echo.unique lines are:
set V
goto :eof

:sub1
  for /L %%j in (1 1 !N!) do (
    if %1 neq %%j if !V%1! equ !V%%j! (
      set R=!R! %1
    )
  )
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#10
September 25, 2010 at 23:08:48
Edit: > 9 version...


Based on the textfile I would use a much more lazy approach:

@echo off
SetLocal EnableDelayedExpansion

rem clear vars..
for /l %%a in (1 1 9) do set #%%a=

for /f "usebackq tokens=*" %%a in ("comfile.txt") do (
    set line=%%a
    2>nul set /a #!line:~3,1! += 1
)

for /l %%a in (1 1 9) do if "!#%%a!"=="1" echo only 1 com%%a
endlocal
pause

Edit:

It occurred to me that mode may spit out com>9 numbers.....

@echo off
SetLocal EnableDelayedExpansion

rem clear vars..
for /f "tokens=1 delims==" %%a in ('2^> nul set #') do set %%a=


for /f "usebackq tokens=*" %%a in ("comfile.txt") do (
    set line=%%a
    2>nul set /a #!line:~0,-1! += 1
)

for /f "tokens=1,2 delims==" %%a in ('2^>nul set #') do (
    if "%%b"=="1" (
        set line=%%a
        echo !line:~1! only occurs once
    )
)

endlocal
pause


Report •

#11
September 27, 2010 at 05:22:09
Judago ,Mechanix2Go, ghostdog
thank you very much, it is very helpful to me.

Report •

Ask Question