中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-06-14 20:56
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [资料]如何提取不同行上的内容
楼 主 [资料]如何提取不同行上的内容 发表于 2006-07-03 10:21 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
积分 1,144
发帖 425
注册 2005-10-20 00:00
UID 43784
来自 北京
状态 离线
中文翻译见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 条) 点击查看详情
评分人分数时间
xiaohacker +1 2007-01-15 05:31
namejm +8 2007-02-06 01:30
2 发表于 2006-07-05 23:37 ·  新加坡
中级用户
★★
积分 249
发帖 64
注册 2005-06-03 00:00
UID 39315
性别 男
状态 离线
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 发表于 2006-07-06 16:07 ·  中国 安徽 安庆 电信
初级用户
积分 36
发帖 14
注册 2006-04-29 14:56
UID 54667
状态 离线
的确就像二楼说的那样 太深奥了 看不懂啊
4 发表于 2006-07-26 22:21 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
积分 1,218
发帖 485
注册 2006-07-21 21:24
UID 58987
来自 湖南.娄底
状态 离线
楼主能不能具体解释一下.
5 发表于 2006-07-27 00:10 ·  中国 广东 中山 广东瑞江科技有限公司BGP数据中心
中级用户
★★
积分 384
发帖 189
注册 2005-10-19 13:12
UID 43709
性别 男
状态 离线
没有中文的说明,只能看懂一部分.
6 发表于 2006-07-27 00:52 ·  中国 辽宁 大连 教育网
中级用户
★★
DOS之友
积分 332
发帖 168
注册 2005-10-06 00:00
UID 43171
性别 男
来自 天涯
状态 离线
英文单词不是太多啊.不认识的可以用百度词典查一下的.而且批处理不会的话.应该把哪个不会说出来.这样也好解释.如果你全篇看不懂的话.那么你不适合看这篇文章.可以看一下我签名中的这个电子书.应该对你有帮助的
测试环境: windows xp pro sp2 高手是这样炼成的:C:\WINDOWS\Help\ntcmds.chm
7 发表于 2006-07-27 11:50 ·  中国 福建 泉州 惠安县 电信
新手上路
积分 14
发帖 5
注册 2006-07-26 13:47
UID 59285
状态 离线
太深奥了 看不懂啊
8 发表于 2006-07-27 14:50 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
积分 6,962
发帖 2,753
注册 2003-04-16 00:00
UID 1565
性别 男
来自 河北保定
状态 离线
很不错,以前还真没有注意到more命令有那么多的参数。
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
9 发表于 2006-07-27 18:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
积分 5,226
发帖 1,737
注册 2006-03-10 00:38
UID 51697
来自 成都
状态 离线
  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
10 发表于 2006-11-23 03:33
中级用户
★★
DOS之日
积分 337
发帖 161
注册 2006-11-04 05:27
UID 69523
性别 男
状态 离线
有点乱
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
11 发表于 2006-11-25 03:19 ·  中国 北京 联通
金牌会员
★★★★
积分 2,902
发帖 1,147
注册 2006-09-21 12:00
UID 63324
性别 男
状态 离线
Originally posted by namejm at 2006-7-27 05:51:
  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。


同感,无限佩服~:)
好玩的贴子不能沉下去~:)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
12 发表于 2007-01-15 03:51 ·  中国 山东 烟台 联通
新手上路
积分 14
发帖 6
注册 2007-01-12 10:08
UID 76276
性别 男
状态 离线
真的很晕,签名里从÷的路径是错了的啊
13 佩服的五体投地 发表于 2007-01-15 05:30 ·  IANA 局域网IP(Private-Use)
初级用户
积分 110
发帖 45
注册 2007-01-07 00:00
UID 75756
性别 男
状态 离线
强人,我佩服的五体投地,但是没有中文说明,我是看的一塌糊涂!唉,请高手们指点一下我等菜们应该看一些什么样的dos技术书!
14 发表于 2007-01-15 06:30 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
积分 2,725
发帖 1,160
注册 2006-09-23 12:00
UID 63486
来自 河北廊坊
状态 离线
灌水

[ Last edited by ccwan on 2007-1-20 at 09:24 PM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
15 发表于 2007-01-21 09:55 ·  中国 陕西 西安 电信
初级用户
积分 46
发帖 21
注册 2006-12-25 14:28
UID 74536
性别 男
来自 西安
状态 离线
说明一下
论坛跳转: