|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『楼 主』:
for语句中如果要处理中间的几行,怎么写skip
使用 LLM 解释/回答一下
大家来讨论下这个问题吧,for语句中如果要处理某个命令输出的中间的几行文字请问要如何处理(比如skip=n是跳过开头n行,如果同时还要跳过最后几行呢,那么这个skip该怎么表达。。。。
Let's discuss this problem. If in a for statement you need to handle the middle several lines of the output of a certain command, how should it be handled? For example, skip=n skips the first n lines. If you also need to skip the last several lines at the same time, then how should this skip be expressed...
|
|
2007-9-16 19:22 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
你可以在处理完了中间的行之后,用一个goto命令跳转到for命令之外。
也可以用 if+for 来处理。
You can use a goto command to jump out of the for command after handling the intermediate lines. You can also use if + for to handle it.
|
|
2007-9-16 21:38 |
|
|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
请问二楼的那你如何判断具体执行到那个位置跳出 能不能举个具体例子
May I ask how you determine exactly which position it jumps out to on the second floor? Can you give a specific example?
|
|
2007-9-16 22:30 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
比如我现在有一个ok.txt文件,内容如下:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
10 10 10 10 10
11 11 11 11 11
我现在只显示第三到第七行的内容,可以用下面这个:
@echo off
rem 处理第三到第七行
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n .* ok.txt') do (
echo 第%%a行 -- %%b
if -%%a==-7 goto :end
)
:end
echo the end
pause
For example, I now have an ok.txt file with the following content:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
10 10 10 10 10
11 11 11 11 11
Now I only display the content from the third to the seventh line, and I can use the following:
@echo off
rem Process lines from the third to the seventh
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n .* ok.txt') do (
echo Line %%a -- %%b
if -%%a==-7 goto :end
)
:end
echo the end
pause
|
|
2007-9-16 23:21 |
|
|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
楼上的例子有点繁 特改如下:
-----------------------------------------------------------------------------------------------
@echo off
rem 处理第三到第七行
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n . ok.txt') do (
echo 第%%a行 -- %%b
if %%a==7 goto :end
)
:end
echo the end
pause
-------------------------------------------------------------------------------------------------
Last edited by renrenrenshk on 2007-9-17 at 09:04 AM ]
The example upstairs is a bit complicated, so it is specially changed as follows:
-----------------------------------------------------------------------------------------------
@echo off
rem Process lines 3 to 7
for /f "skip=2 tokens=1* delims=:" %%a in ('findstr /n . ok.txt') do (
echo Line %%a -- %%b
if %%a==7 goto :end
)
:end
echo the end
pause
-------------------------------------------------------------------------------------------------
Last edited by renrenrenshk on 2007-9-17 at 09:04 AM ]
|
|
2007-9-17 09:01 |
|
|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
楼上的方法还不是非常灵活,比如dir命令的输出结果的行数是经常变的(随着文件夹的不同而不同),只有头尾几行是不变的,如果要处理除了那些不变 的几行外的所有行,那么上面的方法就不适合了
附:
C:\>dir /a-d /-c
驱动器 C 中的卷没有标签。
卷的序列号是 98C6-B5CC
C:\ 的目录
2006-09-22 11:32 0 AUTOEXEC.BAT
2006-09-22 11:25 211 boot.ini
2004-08-17 20:00 322730 bootfont.bin
2006-09-22 11:32 0 CONFIG.SYS
2006-08-12 19:30 0 dfinstall.log
2007-09-17 08:17 266919936 hiberfil.sys
2006-09-22 11:32 0 IO.SYS
2006-09-22 11:32 0 MSDOS.SYS
2004-08-17 20:00 47564 NTDETECT.COM
2004-02-17 14:49 285344 ntldr
2007-09-17 08:28 206 ok.txt
2007-09-17 08:17 402653184 pagefile.sys
2007-09-17 08:18 8871936 Persi0.sys
2006-09-24 11:19 27214 _NavCClt.Log
14 个文件 679128325 字节
0 个目录 11131838464 可用字节
这时如果要处理除开头5行和结尾两行外的所有行那么楼上的方法就不行,不知那为达人 能提供更好的方法.
Last edited by renrenrenshk on 2007-9-17 at 09:15 AM ]
The method above is not very flexible. For example, the number of lines in the output of the dir command often changes (varies with different folders), only the first and last few lines are unchanged. If you want to process all lines except those few unchanged ones, the above method is not suitable.
Attachment:
C:\>dir /a-d /-c
Volume in drive C is not labeled.
Volume Serial Number is 98C6-B5CC
Directory of C:\
2006-09-22 11:32 0 AUTOEXEC.BAT
2006-09-22 11:25 211 boot.ini
2004-08-17 20:00 322730 bootfont.bin
2006-09-22 11:32 0 CONFIG.SYS
2006-08-12 19:30 0 dfinstall.log
2007-09-17 08:17 266919936 hiberfil.sys
2006-09-22 11:32 0 IO.SYS
2006-09-22 11:32 0 MSDOS.SYS
2004-08-17 20:00 47564 NTDETECT.COM
2004-02-17 14:49 285344 ntldr
2007-09-17 08:28 206 ok.txt
2007-09-17 08:17 402653184 pagefile.sys
2007-09-17 08:18 8871936 Persi0.sys
2006-09-24 11:19 27214 _NavCClt.Log
14 File(s) 679128325 bytes
0 Dir(s) 11131838464 bytes free
At this time, if you want to process all lines except the first 5 lines and the last 2 lines, the above method won't work. I wonder if any expert can provide a better method.
Last edited by renrenrenshk on 2007-9-17 at 09:15 AM ]
|
|
2007-9-17 09:12 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
for /f "delims=" %%i in ('dir /a-d /-c^|findstr /r "*--"') do echo %%i
只针对6楼的问题
其实用sed可以很好的解决楼主的问题
Last edited by wudixin96 on 2007-9-17 at 10:31 AM ]
for /f "delims=" %%i in ('dir /a-d /-c^|findstr /r "*--"') do echo %%i
Only for the question on floor 6
In fact, sed can solve the problem of the original poster very well
Last edited by wudixin96 on 2007-9-17 at 10:31 AM ]
|
|
2007-9-17 10:30 |
|
|
terse
银牌会员
    
积分 2404
发帖 946
注册 2005-9-8
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
处理指定行:
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set m=!m:*:=!
if !mn! GEQ 3 if !mn! LEQ 7 echo.!m!>>test2.txt
endlocal
Last edited by terse on 2007-9-18 at 12:30 AM ]
Process specified line:
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set m=!m:*:=!
if !mn! GEQ 3 if !mn! LEQ 7 echo.!m!>>test2.txt
endlocal
Last edited by terse on 2007-9-18 at 12:30 AM ]
|
|
2007-9-17 10:42 |
|
|
6692836
初级用户
 
积分 44
发帖 21
注册 2007-9-12
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
4楼的程序没有问题.学习了
能解释一下for语句中的 delims=:的作用和findstr /n .* ok.txt的语法
我不懂 .谢谢了
Last edited by 6692836 on 2007-9-17 at 04:26 PM ]
The program on floor 4 is okay. I've learned it. Could you explain the role of delims=: in the for statement and the syntax of findstr /n .* ok.txt? I don't understand. Thanks.
Last edited by 6692836 on 2007-9-17 at 04:26 PM ]
|
|
2007-9-17 15:58 |
|
|
6692836
初级用户
 
积分 44
发帖 21
注册 2007-9-12
状态 离线
|
|
2007-9-17 16:30 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by 6692836 at 2007-9-17 15:58:
4楼的程序没有问题.学习了
能解释一下for语句中的 delims=:的作用和findstr /n .* ok.txt的语法
我不懂 .谢谢了
Last edited by 6692836 on 2007-9-17 at 04:26 PM ]
看看 findstr 命令的帮助,看看 /n 参数有什么用,你就会明白为什么这里的 for 会使用 "delims=:" 了.
Originally posted by 6692836 at 2007-9-17 15:58:
The program on floor 4 is okay. Learned it
Can you explain the role of delims=: in the for statement and the syntax of findstr /n .* ok.txt
I don't understand. Thanks
Last edited by 6692836 on 2007-9-17 at 04:26 PM ]
Take a look at the help of the findstr command, see what the /n parameter does, and you will understand why the for here uses "delims=:".
|
|
2007-9-17 16:39 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
我没有评2分的权限,帮不了你了,6692836
I don't have the permission to rate 2 points, so I can't help you. 6692836
|
|
2007-9-17 16:42 |
|
|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
7楼 写的不错 很精辟!!!!!!!!!
-----------------------------------
下面针对九楼和四楼的 发表一下个人的观点。。。不是恶意攻击。。切莫误会。。
我们不是说四楼的代码有没有问题,而是四楼的代码不够精辟,代码中有些多余的东西 让人看了 反而不易理解 就像写程序要讲究算法优化和可读性,四楼的代码就犯了这两个错误。。。
Last edited by renrenrenshk on 2007-9-17 at 06:40 PM ]
7th floor: Well written, very incisive!!!!!
-----------------------------------
The following is my personal view on the 9th and 4th floors... Not malicious attacks. Don't misunderstand.
We are not saying whether there is a problem with the code on the 4th floor, but that the code on the 4th floor is not incisive enough. There are some redundant things in the code that make it difficult to understand. Just like when writing a program, we need to pay attention to algorithm optimization and readability. The code on the 4th floor has made these two mistakes...
Last edited by renrenrenshk on 2007-9-17 at 06:40 PM ]
|
|
2007-9-17 17:50 |
|
|
renrenrenshk
初级用户
 
积分 39
发帖 15
注册 2007-9-16 来自 福建
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
虽然搂住的问题通过各种途径可以解决,但如果for语句中的skip可以完成这项功能,那么实现起来应该会更简单一些。。。 不知skip是否具有这个功能。。。等待达人指导。。。
Last edited by renrenrenshk on 2007-9-17 at 06:17 PM ]
Although the problem of the搂住 can be solved through various ways, if the skip in the for statement can complete this function, then it should be simpler to implement... I don't know if skip has this function... Waiting for experts to guide...
Last edited by renrenrenshk on 2007-9-17 at 06:17 PM ]
|
|
2007-9-17 18:16 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Originally posted by renrenrenshk at 2007-9-17 09:12:
楼上的方法还不是非常灵活,比如dir命令的输出结果的行数是经常变的(随着文件夹的不同而不同),只有头尾几行是不变的,如果要处理除了那些不变 的几 ...
这位兄弟,4楼之前的帖子,你哪一点提到过关于dir命令了?
Originally posted by renrenrenshk at 2007-9-17 09:12:
The method upstairs is not very flexible. For example, the number of lines in the output of the dir command often changes (varies with different folders), only the first and last few lines are unchanged. If you want to process those except the unchanged few...
Brother, in the post before building 4, which point did you mention about the dir command?
|
|
2007-9-17 18:46 |
|