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-24 05:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Data]How to Extract Content on Different Lines DigestI View 12,407 Replies 19
Original Poster Posted 2006-07-03 10:21 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Chinese translation see post 18 (Moderation by HAT @ 2009-01-04)

Originally at #27, corrected to #18 (Administrator note 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
)
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
xiaohacker +1 2007-01-15 05:31
namejm +8 2007-02-06 01:30
Floor 2 Posted 2006-07-05 23:37 ·  新加坡
中级用户
★★
Credits 249
Posts 64
Joined 2005-06-03 00:00
21-year member
UID 39315
Gender Male
Status Offline
对于我们这些新手来说太难了,如果有中文说明就好了。

(注:原句“对一我们新鸟来说太难,要是有中文说明就好了.”中“对一”应为“对于”的笔误,已按正确表述翻译)
Floor 3 Posted 2006-07-06 16:07 ·  中国 安徽 安庆 电信
初级用户
Credits 36
Posts 14
Joined 2006-04-29 14:56
20-year member
UID 54667
Status Offline
Indeed, just like what the second floor said, it's too profound. I can't understand it.
Floor 4 Posted 2006-07-26 22:21 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Can the poster specifically explain?
Floor 5 Posted 2006-07-27 00:10 ·  中国 广东 中山 广东瑞江科技有限公司BGP数据中心
中级用户
★★
Credits 384
Posts 189
Joined 2005-10-19 13:12
20-year member
UID 43709
Gender Male
Status Offline
There is no Chinese explanation, and I can only understand part of it.
Floor 6 Posted 2006-07-27 00:52 ·  中国 辽宁 大连 教育网
中级用户
★★
DOS之友
Credits 332
Posts 168
Joined 2005-10-06 00:00
20-year member
UID 43171
Gender Male
From 天涯
Status Offline
The number of English words is not too many. If you don't know, you can check it with Baidu Dictionary. Also, if you don't know batch processing, you should mention which part you don't understand. That way it's easier to explain. If you can't understand the whole passage, then you may not be suitable for reading this article. You can take a look at this e-book in my signature. It should be helpful to you.
测试环境: windows xp pro sp2 高手是这样炼成的:C:\WINDOWS\Help\ntcmds.chm
Floor 7 Posted 2006-07-27 11:50 ·  中国 福建 泉州 惠安县 电信
新手上路
Credits 14
Posts 5
Joined 2006-07-26 13:47
19-year member
UID 59285
Status Offline
Too profound, can't understand it
Floor 8 Posted 2006-07-27 14:50 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
It's pretty good. I really didn't notice before that the more command has so many parameters.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 9 Posted 2006-07-27 18:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The usage of more is really powerful. In the future, there will be a pattern to follow when displaying the content of specified lines. Hehe, not bad, not bad.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2006-11-23 03:33
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
A bit messy
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 11 Posted 2006-11-25 03:19 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Originally posted by namejm at 2006-7-27 05:51:
The usage of more is really powerful. In the future, there will be a pattern to apply when displaying the content of a specified line. Hehe, not bad not bad.


I feel the same way, extremely admiring~:)
Fun posts shouldn't sink~:)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 12 Posted 2007-01-15 03:51 ·  中国 山东 烟台 联通
新手上路
Credits 14
Posts 6
Joined 2007-01-12 10:08
19-year member
UID 76276
Gender Male
Status Offline
Really confused, the path from ÷ in the signature is wrong
Floor 13 Posted 2007-01-15 05:30 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 110
Posts 45
Joined 2007-01-07 00:00
19-year member
UID 75756
Gender Male
Status Offline
Great people, I admire them completely, but there are no Chinese explanations, and I'm completely confused! Alas, please experts guide us novices what kind of DOS technical books we should read!
Floor 14 Posted 2007-01-15 06:30 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Flood

[ Last edited by ccwan on 2007-1-20 at 09:24 PM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 15 Posted 2007-01-21 09:55 ·  中国 陕西 西安 电信
初级用户
Credits 46
Posts 21
Joined 2006-12-25 14:28
19-year member
UID 74536
Gender Male
From 西安
Status Offline
Let me explain
Forum Jump: