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-06-25 10:41
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] How to grab a specific line of text from a file View 3,171 Replies 12
Original Poster Posted 2005-10-26 01:34 ·  中国 台湾 新北市 中华电信
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
Please ask me the next ipconfig > IP.txt
But I only need the name of the network card with 192.168.1.1
That is, the 4th line above "IP Address. . . . . . . . . . . . : 192.168.1.1"
Set the following string "Realtek RTL8169/8110 Family Gigabi" as a variable
set CardName=Realtek RTL8169/8110 Family Gigabit
How to do it? Because the card name will change, but the first two strings of the IP will not change, that is, 192.168 is fixed. Thank you

Windows 2000 IP Configuration

Host Name . . . . . . . . . . . . : z-h
Primary DNS Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Broadcast
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter Lan:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit
Ethernet NIC
Physical Address. . . . . . . . . : 00-0D-61-7A-99-88
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
DNS Servers . . . . . . . . . . . :

PPP adapter ADSL:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface
Physical Address. . . . . . . . . : 00-53-45-00-33-11
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 61.230.1.1
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : 61.230.1.1
DNS Servers . . . . . . . . . . . : 168.95.1.1
168.95.192.1



[ Last edited by willsort on 2005-10-27 at 20:09 ]
Floor 2 Posted 2005-10-26 09:25 ·  中国 广东 广州 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
It can be achieved by cooperating with the external command LMOD
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 3 Posted 2005-10-26 13:55 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
According to your idea, step - by - step conversion into batch language is as follows:


@echo off
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.1\>"') do (
set line=%%a
)
set /a line-=5
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /a n+=1
if %%a == %line% set CardName=%%c
)
echo.%CardName%
endlocal


Regarding the reason why the set /a line-=5 sentence subtracts 5, experiments find that ipconfig will add line breaks after each sentence, and make its spacing double, and you can measure and reset its value.
Floor 4 Posted 2005-10-26 14:53 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re JonePeng:

Strictly speaking, LMOD is not an external command, but a third-party tool software. Therefore, it is not included in the installation disks of MSDOS6.22/7.10.

Re 无奈何:

It is not necessary to start taking values from 192.168.1.1. You can directly find "Description" and then take the value of the first line. For example:

for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2005-10-26 15:49 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
: For machines with dual or multiple network cards, this might get an unexpected value. I think the building - owner's train of thought is still prudent.
Floor 6 Posted 2005-10-26 20:11 ·  中国 台湾 新北市 中华电信
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
Originally posted by willsort at 2005-10-26 14:53:
Re 无奈何:
It's not necessary to start with 192.168.1.1 to get the value. You can directly find "Description" and take the value of the first line. For example:
for /f "delims=: tokens=2" %s in ('ipconfig /all ^| find "Des" ^| find /v "WAN"') do @set CardName=%s

Moderator, I mentioned earlier why we need to start with 192.168. Because a computer may have more than two network cards, but only that IP is fixed. So.. we must start from that line to calculate to get the correct name of the network card.
Is there any way to modify it?


Your batch file can execute correctly, but you need to change %s to %%s^^"

[ Last edited by dosfroum on 2005-10-26 at 20:56 ]
Floor 7 Posted 2005-10-26 20:28 ·  中国 台湾 新北市 中华电信
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
Originally posted by Helpless at 2005-10-26 13:55:
Just follow your train of thought and convert it step by step into batch language. The following is the batch implementation according to your train of thought:

Can't move, can't move, a bunch of errors and haven't figured out the reason. Can you explain it roughly? Thanks
I have tried very hard to check, but can't see the clue
delims=xxx - Specify the set of delimiters. This will replace
the default set of delimiters which are whitespace and tab characters.
tokens=x,y,m-n - Specify which strings in each line should be
passed to the internal part of for for each repeated operation.
This will result in additional variable names being allocated.
The m-n format represents a range,
specifying the m-th to n-th string items.
If the last character in the tokens= string is
an asterisk, then an additional variable
will be allocated to receive the other text after
the last string item is analyzed.



findstr /?
Searches for strings in files.

FINDSTR
filename]

/B Matches if at the beginning of a line.
/E Matches if at the end of a line.
/L Uses the search strings literally.
/R Uses the search string as a regular expression.
/S Searches for matching files in the current directory and all subdirectories.
/I Specifies that the search is case-insensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain the matching string.
/N Prints the line number before each line that matches.
/M Prints only the file names of files that contain the matching string.
/O Prints the character offset before each matching line.
/P Skips files with non-printable characters.
/F:file Reads file list from the specified file (/ represents the console).
/C:string Uses the specific string as a literal search string.
/G:file Gets search strings from the specified file (/ represents the console).
strings The text to search for.
filename
Specifies one or more files to search.

Unless the prefix of the argument is /C, separate multiple search
strings with spaces. For example, 'FINDSTR "hello there" x.y' will search for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' will search for
"hello there" in file x.y.




@echo on
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168.1.2\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (set /a n+=1 if %%a == %line% set CardName=%%c)
echo. %CardName%
endlocal


E:\>setlocal

E:\>for /F "tokens=1,* delims=:" %a in ('ipconfig /all ^|findstr/r /n "\<192.168.
1.2\>"') do (set line=%a )

E:\>(set line=16 )

E:\>set /a line-=3

E:\>echo.13
13

E:\>set CardName=

E:\>for /F "tokens=1,2,3 delims=:" %a in ('ipconfig /all ^|findstr/r /n "."') do
(set /a n+=1 if %a == 13 set CardName=%c )

E:\>(set /a n+=1 if 2 == 13 set CardName= )
Missing operand.

E:\>(set /a n+=1 if 4 == 13 set CardName= z-h )
Missing operand.

E:\>(set /a n+=1 if 5 == 13 set CardName= )
Missing operand.

) >(set /a n+=1 if 6 == 13 set CardName= Broadcast
Missing operand.

) >(set /a n+=1 if 7 == 13 set CardName= No
Missing operand.

) >(set /a n+=1 if 8 == 13 set CardName= No
Missing operand.

) >(set /a n+=1 if 10 == 13 set CardName=
Missing operand.

E:\>(set /a n+=1 if 12 == 13 set CardName= )
Missing operand.

E:\>(set /a n+=1 if 13 == 13 set CardName= Realtek RTL8169/8110 Family Gigabit E
thernet NIC )
Missing operand.

) >(set /a n+=1 if 14 == 13 set CardName= 00-0D-61-7A-88-54
Missing operand.

) >(set /a n+=1 if 15 == 13 set CardName= No
Missing operand.

) >(set /a n+=1 if 16 == 13 set CardName= 192.168.1.2
Missing operand.

) >(set /a n+=1 if 17 == 13 set CardName= 255.255.255.0
Missing operand.

) >(set /a n+=1 if 18 == 13 set CardName=
Missing operand.

E:\>(set /a n+=1 if 19 == 13 set CardName= )
Missing operand.

) >(set /a n+=1 if 21 == 13 set CardName=
Missing operand.

E:\>(set /a n+=1 if 23 == 13 set CardName= )
Missing operand.

) >(set /a n+=1 if 24 == 13 set CardName= WAN (PPP/SLIP) Interface
Missing operand.

) >(set /a n+=1 if 25 == 13 set CardName= 00-53-45-00-00-00
Missing operand.

) >(set /a n+=1 if 26 == 13 set CardName= No
Missing operand.

) >(set /a n+=1 if 27 == 13 set CardName= 61.230.197.208
Missing operand.

) >(set /a n+=1 if 28 == 13 set CardName= 255.255.255.255
Missing operand.

) >(set /a n+=1 if 29 == 13 set CardName= 61.230.197.208
Missing operand.

E:\>(set /a n+=1 if 30 == 13 set CardName= 168.95.1.1 )
Missing operand.

E:\>(set /a n+=1 if 31 == 13 set CardName= )
Missing operand.

E:\>echo.


E:\>endlocal

E:\>


[ Last edited by dosfroum on 2005-10-26 at 21:10 ]
Floor 8 Posted 2005-10-26 21:31 ·  中国 台湾 新北市 中华电信
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
Got it. The position of () has a big impact


@echo off
cls
setlocal
for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr/r /n "\<192.168\>"') do (set line=%%a)
set /a line-=3
echo.%line%
set CardName=
for /f "tokens=1,2,3 delims=:" %%a in ('ipconfig /all ^|findstr/r /n "."') do (
set /A n+=1
if %%a == %line% set CardName=%%c
)
echo. %CardName%
endlocal


Follow-up question: Can you help explain
delims=xxx tokens=x,y,m-n these two parameters, what does %%c represent
Because I want to try to display the IP of that line but it always shows the previous things, please help solve the confusion


for /f "tokens=1,* delims=:" %%a in ('ipconfig /all ^|findstr /r "192.168"') do (set IPaddr=%%a)
echo. %IPaddr%

The result only shows like this @@a

IP Address. . . . . . . . . . . .
Floor 9 Posted 2005-10-26 21:44 ·  中国 台湾 新北市 中华电信
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
Summary arrangement
What does setlocal, endlocal <<< mean??
delims=: means to separate with colons
tokens=2 means that after separating into two segments with colons, take the latter segment
What does tokens=1,2,3 <<< mean??
Where does %%c <<<<<< come from
There is an extra space in the captured card name
My solution is
set IA=%IPaddr:~1%

Is there a better way?

[ Last edited by dosfroum on 2005-10-26 at 21:56 ]
Floor 10 Posted 2005-10-26 23:25 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
1、setlocal and endlocal indicate using their own environment variables within the interval.
2、tokens=1,2,3 delims=: %%a means to get the first 3 strings split by : and assign them to %%a, %%b, %%c respectively, and the last two are automatically assigned by the system.
3、For the extra blank characters, just exclude them when taking the substring like you do.

Actually, the help for the problem is all explained in the online help. Read the content displayed by for /? several times. The for command is a very powerful command and it's worth taking some time to learn.
Floor 11 Posted 2005-10-27 01:28 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 无奈何 & dosfroum:

The situation with dual network cards does indeed have problems. It should be very difficult to encounter multiple network cards? But multiple network connections are very likely, so some changes need to be made. But what does the 192.168 network card indicate? Does it mean it's the local connection network card (LAN card)? My local network card starts with 10.0.

The example I wrote on the 4th floor is run in the command line, not a batch script. You can tell from the @ before set; if it were a batch script, I would make it more complete and rigorous in structure and add [code] and [/code] code modifiers.

As for Brother 无奈何's code, it can still be simplified. There's no need to get the line number; just use one FOR to extract it. The idea is as follows: Analyze each line of the IPCONFIG output. If Description... is encountered, take the subsequent network card name. If 192.168... is encountered, jump out of the loop. The specific code won't be provided.

Removing leading whitespace is simplest with set; the line to get the IP is because of the tokens issue. It should be tokens=2, so %%a will be assigned the value of the 2nd "token" (the substring of the string split by delims). Your usage assigned the IP address to %%b.

As for the Chinese online help for FOR, I suggest only looking at the first half. The second half is obviously a machine translation result that hasn't been corrected. It's not only grammatically obscure but also the example for usebackq is incorrect, so it's better not to look at it. Of course, if your English level is okay, you can chcp 437 and browse the English version.

FOR is very powerful, and therefore has many bugs. My suggestion is to use usebackq as little as possible, otherwise problems are easy to encounter. Moreover, I think there's no need to concentrate many functions in one FOR command. This will only increase the complexity and reduce the robustness of the command. If they are split into several separate commands according to functions, it's beneficial for users to learn and for programming implementation.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 12 Posted 2005-10-28 00:31 ·  中国 台湾 中华电信(HiNet)
初级用户
★★
Credits 154
Posts 54
Joined 2005-09-25 01:53
20-year member
UID 42804
Status Offline
There is an answer, but can I continue to ask?
Moderator willsort, the idea you mentioned, I have thought about it for an afternoon, and I have no clue.
Then what was I doing for the whole morning? Thinking about another problem. Because the network card of some computers will catch #2 #3....
But I just want the first string of characters. Don't want #2. I thought of using set to deduct the subsequent numbers, but if there is no #2 state, the card name will be truncated.
Attached with the 192.168 network card, it can't explain anything. It's just one of the rules we set, so we can just find it from this string of characters.
Floor 13 Posted 2005-10-28 10:53 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re: dosfroum:

Then try this program:


:: GetCard.cmd - 获取IP地址为“192.168.*.*”的网卡的“描述”
:: Will Sort - 2005-10-28 - CMD@WinXP
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%s in ('ipconfig /all') do (
set line=%%s
echo.!line! | findstr "Description" >nul && set CardName=!line:~44!
echo.!CardName! | findstr /r "\<#" >nul && set CardName=!CardName:~0,-3!
echo.!line! | findstr "192.168." >nul && goto EchoName
)
:EchoName
echo CardName="%CardName%"
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: