|
mp3down
初级用户
 
积分 43
发帖 14
注册 2006-12-9
状态 离线
|
『楼 主』:
请问如果在文本中抽取指定字符赋值于变量中
使用 LLM 解释/回答一下
我想,在一个文本文件中,抽取第17行第8-20这一串字符,并将它们赋值于一个变量给接下来的命令用,怎么办?
请各位大侠不要怪我整天问这些太菜的问题,我也觉得有点不好意思。想到不耻下问,这坛子的确令我学到东西,所以就壮胆了。
Last edited by namejm on 2006-12-13 at 01:54 PM ]
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 ]
|
|
2006-12-12 06:06 |
|
|
a9319751
中级用户
  
积分 439
发帖 170
注册 2006-1-9
状态 离线
|
|
2006-12-12 06:12 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
  『第 3 楼』:
使用 LLM 解释/回答一下
@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
或者:
@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 ]
@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 ]
此帖被 +5 点积分 点击查看详情 评分人:【 ccwan 】 | 分数: +5 | 时间:2006-12-12 06:24 |
|
|
|
2006-12-12 06:19 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
|
2006-12-12 06:21 |
|
|
NaturalJ0
银牌会员
    
积分 1181
发帖 533
注册 2006-8-14
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
哇,我手脚太慢了。 = =b
Wow, I'm too slow with my hands and feet. = =b
|
|
2006-12-12 06:22 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
我的手够快吧?
鼓励一下热心的朋友们!
Am I fast enough with my hands?
Give a little encouragement to the enthusiastic friends!
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-12 06:24 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
还有,楼主注意你的标题!
Also, the poster, please pay attention to your title!
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-12-12 06:53 |
|
|
mp3down
初级用户
 
积分 43
发帖 14
注册 2006-12-9
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
谢谢热心的朋友们,我以后会注意标题了。
Thank you, kind friends. I will pay attention to the title in the future.
|
|
2006-12-12 07:08 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Re all:
用批处理来提取文本内容,要随时注意兼容特殊字符,比如空格,比如连接字符&、||等,因此,youxi01 兄在3F的代码动用了 delims= 和 set "str=%%i" ,这是非常有必要的。
不过,批处理还是不能处理所有的特殊情况,比如:如果有以半角分号开头的行,一般的 for 语句会忽略这些行,用 set /a num+=1 来统计行数会出错,换成 for + findstr /n 就可以避免;但是,for + findstr /n 也不能包打天下,至少,如果某些行以半角冒号打头,则 for /f "tokens=1* delims=:" %%i in ('findstr /n .* test.txt') do echo "%%j" 会把所有行首冒号忽略掉。所以,在处理文本的时候,只能指望这些特殊的情形不一起出现了。不知各位有没有什么好办法来解决。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-12 09:07 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
@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
暂时的变更方案,试试有无bug。
```@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.```
|
|
2006-12-12 10:25 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
3742668 兄10F的代码仍然不能正确处理行首为分号的情况,也不能正确处理行内带感叹号的情况。
代码1能正确处理行首为分号或冒号,行上带&、||等特殊字符的文本,但是不能正确提取带感叹号的内容,代码2能解决感叹号的问题,但是需要用引号括起来,基本上解决了我在9F提出的问题,请各位测试:
代码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
代码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
test.txt的内容:
:abc
::xy
;
;;;ef &
;:;
::;;
||
&
^
!<
!
%
"
""
'
Last edited by namejm on 2006-12-11 at 10:13 PM ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-12 11:04 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
|
2006-12-12 11:41 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
eol= 用得巧,setlocal 用得妙,高,实在是高,嘿嘿。
eol= Use it cleverly, setlocal is wonderful, great, really great, heh heh.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-12-12 12:18 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
|
2006-12-12 13:47 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
还有一个问题,就是如果a.txt里面有空行(直接回车)的话,那么就会出错,批处理提示:
已经达到最大的 setlocal 递归层。
不知道这个问题怎么解决?
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?
|
|
2006-12-12 13:50 |
|