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-22 12:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问如果在文本中抽取指定字符赋值于变量中 Please ask if you want to extract specified characters from text and assign them to variables View 5,750 Replies 32
Original Poster Posted 2006-12-12 06:06 ·  中国 广东 深圳 电信
初级用户
Credits 43
Posts 14
Joined 2006-12-09 06:08
19-year member
UID 73009
Gender Male
Status Offline
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 ]
Floor 2 Posted 2006-12-12 06:12 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
```
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
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-12 06:23
bat c c++
Floor 3 Posted 2006-12-12 06:19 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline

@echo off
for /f "skip=16 delims=" %%i in (test.txt) do (
set "str=%%i"
call set "str=%%str:~7,12%%"
goto :exit)
:exit
echo %str%
pause>nul

or:


@echo off
for /f "delims=: tokens=1,2*" %%i in ('findstr /n . test.txt') do (
if %%i EQU 17 (
set "str=%%j"
call set "str=%%str:~7,12%%"
goto :exit))
:exit
echo %str%
pause>nul



[ Last edited by youxi01 on 2006-12-12 at 06:22 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-12 06:24
Floor 4 Posted 2006-12-12 06:21 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2006-12-12 06:24
Floor 5 Posted 2006-12-12 06:22 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Wow, I'm too slow with my hands and feet. = =b
Floor 6 Posted 2006-12-12 06:24 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Am I fast enough with my hands?
Give a little encouragement to the enthusiastic friends!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2006-12-12 06:53 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Also, the poster, please pay attention to your title!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2006-12-12 07:08 ·  中国 广东 深圳 电信
初级用户
Credits 43
Posts 14
Joined 2006-12-09 06:08
19-year member
UID 73009
Gender Male
Status Offline
Thank you, kind friends. I will pay attention to the title in the future.
Floor 9 Posted 2006-12-12 09:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-12-12 10:25 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
```@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.```
Floor 11 Posted 2006-12-12 11:04 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
set num=0
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a num+=1
set "str=%%i"
echo 第 !num! 行: !str:*:=!
)
pause

Code 2:

@echo off
set num=0
::setlocal enabledelayedexpansion
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a num+=1
set "str=%%i"
call :display
)
pause
goto :eof

:display
echo 第 %num% 行: "%str:*:=%"
goto :eof

The content of test.txt:

:abc
::xy
;
;;;ef &
;:;
::;;
||
&
^
!<
!
%
"
""
'


[ Last edited by namejm on 2006-12-11 at 10:13 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-12-12 11:41 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
试试这个:

@echo off
setlocal enabledelayedexpansion
set num=
for /f "eol= delims=" %%i in ('findstr .* a.txt') do (
setlocal DISABLEDELAYEDEXPANSION
set "tmpstr=%%i"
setlocal enabledelayedexpansion
set /a num += 1
set "!num!=!tmpstr!"
)
for /l %%i in (1,1,%num%) do echo 第%%i行: !%%i!
pause
exit /b 0
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lxmxn +5 2006-12-12 13:47
Floor 13 Posted 2006-12-12 12:18 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
eol= Use it cleverly, setlocal is wonderful, great, really great, heh heh.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2006-12-12 13:47 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Really powerful. Learned. .
Floor 15 Posted 2006-12-12 13:50 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

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?
Forum Jump: