中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-09 00:19
47,811 主题排行 / 349,895 发帖 / 今日 0 篇 / 48,253 会员排行
DOS批处理 & 脚本技术(批处理室) » [资料]如何提取不同行上的内容
可打印版本  12,830 / 19
第1楼 bagpipe 发表于 2006-07-03 10:21
银牌会员 发帖 425 积分 1,144 来自 北京
[资料]如何提取不同行上的内容
中文翻译见18楼(版务 by HAT @ 2009-01-04)

原在#27,修正至#18 (管理员注 2009-1-5)


This page shows how to read specific lines from a text file. There are many ways to have the for /f command read the input file, for instance:-

for /f "delims=" %%a in (input.txt) do ...

for /f "delims=" %%a in ('type input.txt') do ...

for /f "delims=" %%a in ('more ^< input.txt') do ...

However, only the last method (using the more command) will give consistent results across Windows NT, 2000, XP and 2003. The first method does not recognise unicode files. Also, the usebackq switch must be used if the input filename contains spaces. The second method, using the type command, also fails to recognise unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM).

In all the examples, assume the contents of of the file numbers.txt to be:-

one
two
three
four
five
six
seven
eight
nine
ten

Displaying the first line

This example prints one.

@echo off & setlocal ENABLEEXTENSIONS
set "first="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
if not defined first set first=%%a
)
echo/%first%

Displaying the first X lines

This example prints one, two and three.

@echo off & setlocal ENABLEEXTENSIONS
set "lines=3"
set i=-1
set "ok="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)

Displaying the last line

This example prints ten.

@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%

Displaying the last X lines

This example prints nine and ten.

@echo off & setlocal ENABLEEXTENSIONS
set "lines=2"
for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
echo/%%a
)

Displaying the Nth line

This example prints three. Note that instead of using the more command's /e switch, the skip option could have been used with the for /f command, however, this fails is it is set to any number less than one.

@echo off & setlocal ENABLEEXTENSIONS
set LineNo=3
set "line="
set/a LineNo-=1
for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
if not defined line set "line=%%a"
)
echo/%line%

Displaying the Nth line plus X number of lines

This example prints five and six.

@echo off & setlocal ENABLEEXTENSIONS
set start=5
set "lines=2"
set/a i=-1,start-=1
set "ok="
for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)
第2楼 tclshx 发表于 2006-07-05 23:37
中级用户 发帖 64 积分 249
Originally posted by bagpipe at 2006-7-3 10:21 AM:
This page shows how to read specific lines from a text file. There are many ways to have the for /f command read the input file, for instance:-

for /f "delims=" %%a in (input.txt) do . ...



对一我们新鸟来说太难,要是有中文说明就好了.
第3楼 hanbsome 发表于 2006-07-06 16:07
初级用户 发帖 14 积分 36
的确就像二楼说的那样 太深奥了 看不懂啊
第4楼 pengfei 发表于 2006-07-26 22:21
银牌会员 发帖 485 积分 1,218 来自 湖南.娄底
楼主能不能具体解释一下.
第5楼 voiL 发表于 2006-07-27 00:10
中级用户 发帖 189 积分 384
没有中文的说明,只能看懂一部分.
第6楼 IceCrack 发表于 2006-07-27 00:52
中级用户 发帖 168 积分 332 来自 天涯
英文单词不是太多啊.不认识的可以用百度词典查一下的.而且批处理不会的话.应该把哪个不会说出来.这样也好解释.如果你全篇看不懂的话.那么你不适合看这篇文章.可以看一下我签名中的这个电子书.应该对你有帮助的
第7楼 qwr123 发表于 2006-07-27 11:50
新手上路 发帖 5 积分 14
太深奥了 看不懂啊
第8楼 Climbing 发表于 2006-07-27 14:50
铂金会员 发帖 2,753 积分 6,962 来自 河北保定
很不错,以前还真没有注意到more命令有那么多的参数。
第9楼 namejm 发表于 2006-07-27 18:51
荣誉版主 发帖 1,737 积分 5,226 来自 成都
  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。
第10楼 hxuan999 发表于 2006-11-23 03:33
中级用户 发帖 161 积分 337
有点乱
第11楼 redtek 发表于 2006-11-25 03:19
金牌会员 发帖 1,147 积分 2,902
Originally posted by namejm at 2006-7-27 05:51:
  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。


同感,无限佩服~:)
好玩的贴子不能沉下去~:)
第12楼 foxfast 发表于 2007-01-15 03:51
新手上路 发帖 6 积分 14
真的很晕,签名里从÷的路径是错了的啊
第13楼 xiaohacker 发表于 2007-01-15 05:30
初级用户 发帖 45 积分 110
佩服的五体投地
强人,我佩服的五体投地,但是没有中文说明,我是看的一塌糊涂!唉,请高手们指点一下我等菜们应该看一些什么样的dos技术书!
第14楼 ccwan 发表于 2007-01-15 06:30
金牌会员 发帖 1,160 积分 2,725 来自 河北廊坊
灌水

[ Last edited by ccwan on 2007-1-20 at 09:24 PM ]
第15楼 40szb 发表于 2007-01-21 09:55
初级用户 发帖 21 积分 46 来自 西安
说明一下
1 2  下一页
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023