中国DOS联盟论坛

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-08 11:23
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » 请问如果在文本中抽取指定字符赋值于变量中 Please ask if you want to extract specified characters from text and assign them to variables
Printable Version  6,038 / 32
Floor1 mp3down Posted 2006-12-12 06:06
初级用户 Posts 14 Credits 43
I want to extract the characters from position 8 to 20 of line 17 in a text file and assign them to a variable for subsequent commands.

Please don't blame me for asking such too-simple questions all day, I also feel a bit embarrassed. Thinking of asking regardless of dignity, this forum has indeed made me learn things, so I plucked up the courage.

[ Last edited by namejm on 2006-12-13 at 01:54 PM ]
Floor2 a9319751 Posted 2006-12-12 06:12
中级用户 Posts 170 Credits 439
```
setlocal enabledelayedexpansion
for /f "delims=" %%i in (1.txt ) do (
set /a num+=1
if !num! equ 17 set var=%%i
if !num! equ 17 set var=!var:~7,13!
)
echo !var!
pause
```
Floor3 youxi01 Posted 2006-12-12 06:19
高级用户 Posts 247 Credits 846 From 湖南==》广东

or:


[ Last edited by youxi01 on 2006-12-12 at 06:22 AM ]
Floor4 NaturalJ0 Posted 2006-12-12 06:21
银牌会员 Posts 533 Credits 1,181
setlocal enabledelayedexpansion
for /f "skip=16 delims=" %%i in (filename.txt) do (
set var=%%i
set var=!var:~7,13!
goto :outfor)
:outfor

The red parts you can replace at will.
Floor5 NaturalJ0 Posted 2006-12-12 06:22
银牌会员 Posts 533 Credits 1,181
Wow, I'm too slow with my hands and feet. = =b
Floor6 ccwan Posted 2006-12-12 06:24
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Am I fast enough with my hands?
Give a little encouragement to the enthusiastic friends!
Floor7 ccwan Posted 2006-12-12 06:53
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Also, the poster, please pay attention to your title!
Floor8 mp3down Posted 2006-12-12 07:08
初级用户 Posts 14 Credits 43
Thank you, kind friends. I will pay attention to the title in the future.
Floor9 namejm Posted 2006-12-12 09:07
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Re all:

  To extract text content with batch processing, special characters such as spaces, and connection characters like &, ||, etc., should be noted for compatibility at any time. Therefore, Brother youxi01's code in 3F uses delims= and set "str=%%i", which is very necessary.

  However, batch processing still cannot handle all special cases. For example, if there are lines starting with a half-width semicolon, the general for statement will ignore these lines, and counting the number of lines with set /a num+=1 will be wrong. Using for + findstr /n can avoid this; but for + findstr /n cannot cover all situations. At least, if some lines start with a half-width colon, for /f "tokens=1* delims=:" %%i in ('findstr /n .* test.txt') do echo "%%j" will ignore all colons at the beginning of the line. So, when processing text, we can only hope that these special situations do not occur together. I wonder if you all have any good solutions.
Floor10 3742668 Posted 2006-12-12 10:25
荣誉版主 Posts 718 Credits 2,013
```@echo off
setlocal enabledelayedexpansion
set num=
for /f "delims=" %%i in ('findstr .* a.txt') do (
set /a num += 1
set "!num!=%%i"
)
for /l %%i in (1,1,%num%) do echo 第%%i行: !%%i!
pause
Temporary change plan, try to see if there are bugs.```
Floor11 namejm Posted 2006-12-12 11:04
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Brother 3742668, the code in 10F still can't correctly handle the situation where there's a semicolon at the beginning of a line, nor can it handle the situation where there's an exclamation mark within a line.

Code 1 can correctly handle text with a semicolon or colon at the beginning of a line and special characters like &, ||, etc. in the line, but it can't correctly extract content with an exclamation mark. Code 2 can solve the problem of the exclamation mark, but it needs to be enclosed in quotes. It basically solves the problem I raised in 9F. Please test everyone:

Code 1:

Code 2:

The content of test.txt:


[ Last edited by namejm on 2006-12-11 at 10:13 PM ]
Floor12 3742668 Posted 2006-12-12 11:41
荣誉版主 Posts 718 Credits 2,013
试试这个:

Floor13 namejm Posted 2006-12-12 12:18
荣誉版主 Posts 1,737 Credits 5,226 From 成都
eol= Use it cleverly, setlocal is wonderful, great, really great, heh heh.
Floor14 lxmxn Posted 2006-12-12 13:47
版主 Posts 4,938 Credits 11,386

  Really powerful. Learned. .
Floor15 lxmxn Posted 2006-12-12 13:50
版主 Posts 4,938 Credits 11,386

There is another problem. If there are blank lines (just pressing Enter) in a.txt, then an error will occur, and the batch processing prompts:

The maximum setlocal recursion level has been reached.

I don't know how to solve this problem?
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023