![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-09 23:51 |
47,811 topics / 349,897 posts / today 2 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » [Solved] How to grab a specific line of text from a file |
| Printable Version 3,383 / 12 |
| Floor1 dosfroum | Posted 2005-10-26 01:34 |
| 初级用户 Posts 54 Credits 154 | |
|
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 [ Last edited by willsort on 2005-10-27 at 20:09 ] |
|
| Floor2 JonePeng | Posted 2005-10-26 09:25 |
| 金牌会员 Posts 1,883 Credits 4,562 From 广东广州 | |
|
It can be achieved by cooperating with the external command LMOD
|
|
| Floor3 无奈何 | Posted 2005-10-26 13:55 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
According to your idea, step - by - step conversion into batch language is as follows:
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. |
|
| Floor4 willsort | Posted 2005-10-26 14:53 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 |
|
| Floor5 无奈何 | Posted 2005-10-26 15:49 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
: 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.
|
|
| Floor6 dosfroum | Posted 2005-10-26 20:11 |
| 初级用户 Posts 54 Credits 154 | |
Originally posted by willsort at 2005-10-26 14:53: 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 ] |
|
| Floor7 dosfroum | Posted 2005-10-26 20:28 |
| 初级用户 Posts 54 Credits 154 | |
Originally posted by Helpless at 2005-10-26 13:55: 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. [ Last edited by dosfroum on 2005-10-26 at 21:10 ] |
|
| Floor8 dosfroum | Posted 2005-10-26 21:31 |
| 初级用户 Posts 54 Credits 154 | |
|
Got it. The position of () has a big impact
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 The result only shows like this @@a |
|
| Floor9 dosfroum | Posted 2005-10-26 21:44 |
| 初级用户 Posts 54 Credits 154 | |
|
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 Is there a better way? [ Last edited by dosfroum on 2005-10-26 at 21:56 ] |
|
| Floor10 无奈何 | Posted 2005-10-26 23:25 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
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. |
|
| Floor11 willsort | Posted 2005-10-27 01:28 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor12 dosfroum | Posted 2005-10-28 00:31 |
| 初级用户 Posts 54 Credits 154 | |
|
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. |
|
| Floor13 willsort | Posted 2005-10-28 10:53 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re: dosfroum:
Then try this program: |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |