China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 21:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » {Solved} How to compare file contents - for comparing MAC addresses View 3,573 Replies 22
Original Poster Posted 2007-03-10 11:01 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
The content in the question is mainly about how to write a batch processing program to compare the MAC addresses in two text files and handle the situation where the MAC address changes. The following is a possible batch processing code idea (for reference only, may need to be adjusted according to actual situations):

```batch
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:" %%a in (a.txt) do (
set "ip_a=%%a"
set "mac_a=%%b"
for /f "tokens=1,2 delims=:" %%c in (b.txt) do (
if "%%c"=="!ip_a!" (
if not "%%d"=="!mac_a!" (
echo!ip_a!:!mac_a!>>1.txt
)
goto next
)
)
:next
)
endlocal
```

Please note that this is a simple implementation idea. In actual use, it may need to be further optimized and adjusted according to more complex actual situations.
学无尽止
Floor 2 Posted 2007-03-10 12:28 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Other projects, such as: 192.168.2.1, 192.168.2.3, etc. Do you not need to check?
Floor 3 Posted 2007-03-10 12:32 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

@echo off
if exist 1.txt del 1.txt
for /f "delims=: tokens=1*" %%a in (a.txt) do (
for /f "delims=" %%_ in ('findstr "%%a" b.txt') do (
if NOT "%%a:%%b"=="%%_" >>1.txt echo %%_
)
)
start 1.txt


[最后编辑于2007-3-10 下午06:10由lxmxn]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
chainliq +3 2007-03-10 23:09
Floor 4 Posted 2007-03-10 12:53 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Also post a paragraph to play:


@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:" %%i in (a.txt) do set "%%i=%%j"

echo ============================
echo The MAC changes in documents a.txt and b.txt are:
echo.
for /f "tokens=1,2 delims=:" %%i in (b.txt) do (
if /i not "!%%i!"=="%%j" (
echo %%i %%j
if "%%i"=="192.168.2.2" set flag=1
)
)
echo -----------------------------
echo Among them:
if defined flag (echo The MAC of 192.168.2.2 has been changed.) else echo The MAC of 192.168.2.2 has not been changed.
echo ==============================

pause>nul
Floor 5 Posted 2007-03-10 22:34 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
Originally posted by youxi01 at 2007-3-10 12:28:
Other items, such as: 192.168.2.1, 192.168.2.3, etc., do they not need to be checked?



Need to, otherwise, it would be easy to do, ``What needs to be checked are hundreds of MACs``
学无尽止
Floor 6 Posted 2007-03-10 22:39 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
Brother lxmxn, it seems like it didn't respond after running.
学无尽止
Floor 7 Posted 2007-03-10 22:57 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```
@echo off
for /f "tokens=1* delims=-" %%i in (a.txt) do (
for /f "tokens=1* delims=-" %%a in (b.txt) do (
if "%%i"=="%%a" if not "%%j"=="%%b" >>1.txt echo %%a-%%b
)
)
pause
```
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2007-03-10 23:07 ·  中国 湖北 武汉 电信
初级用户
Credits 22
Posts 10
Joined 2007-02-16 22:46
19-year member
UID 79647
Gender Male
Status Offline
@echo off
rem The order of IP can be scrambled
setlocal ENABLEDELAYEDEXPANSION
set t1=!time!

set /a num=1
for /f "tokens=1,2 delims=:" %%i in (b.txt) do (
set ip!num!=%%i
set mac!num!=%%j
set /a num+=1
)

for /f "tokens=1,2 delims=:" %%i in (a.txt) do (
for /L %%a in (1,1,!num!) do (
if "!ip%%a!"=="%%i" (
if not "!mac%%a!"=="%%j" echo !ip%%a!:!mac%%a! vs %%i:%%j
)
)
)


echo Number of comparison lines:%num%
echo Start time:%t1%
echo End time:!time!
Floor 9 Posted 2007-03-10 23:13 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
Hehe, there are really many experts. Well, I didn't use it properly just now. Actually, Brother lxmxn's one can be used. Thanks to everyone. The others are also good. Well, ashamed. I need to learn from you all. I tried, and they all work. hehe
学无尽止
Floor 10 Posted 2007-03-10 23:35 ·  中国 湖北 武汉 电信
初级用户
Credits 22
Posts 10
Joined 2007-02-16 22:46
19-year member
UID 79647
Gender Male
Status Offline
The code on the 4th floor is really extremely efficient. I admire it!
Floor 11 Posted 2007-03-11 02:41 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Originally posted by test266 at 2007-3-10 10:35:
The code on floor 4 is really efficient, I admire it!


Sweat~ #4 seems to only check whether that one is equal... If you change others, it doesn't care... A failed code... Don't mention efficiency.

◎echo off
echo XXX.XX.XX.X
exit
What about this efficiency? Sweat~
Floor 12 Posted 2007-03-11 03:14 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
The brother who went upstairs seems to have not understood the code.
Floor 13 Posted 2007-03-11 03:34 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
if defined flag (echo 192.168.2.2's MAC has been changed.) else echo 192.168.2.2's MAC has not been changed.

What would your code output if 192.168.2.3 changes?

Either do both or don't be redundant. Just echo %%i %j directly.

Have you tried the fc a.txt b.txt command?
Floor 14 Posted 2007-03-11 03:48 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Don't argue anymore. Everyone has different ways to solve problems, which is a good thing and can broaden ideas, isn't it?

Let me solve it with one sentence.
findstr /v /g:b.txt a.txt >1.txt
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 15 Posted 2007-03-11 04:35 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Hehe, back.

The original meaning of my text is to first find out all the changed macs,
and then judge whether the mac of IP 192.168.2.2 has changed.

The original idea is that this might be more in line with the landlord's meaning, maybe I was wrong.
Forum Jump: