中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-08 21:14
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Data]How to Extract Content on Different Lines
Printable Version  12,820 / 19
Floor1 bagpipe Posted 2006-07-03 10:21
银牌会员 Posts 425 Credits 1,144 From 北京
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
)
Floor2 tclshx Posted 2006-07-05 23:37
中级用户 Posts 64 Credits 249
对于我们这些新手来说太难了,如果有中文说明就好了。

(注:原句“对一我们新鸟来说太难,要是有中文说明就好了.”中“对一”应为“对于”的笔误,已按正确表述翻译)
Floor3 hanbsome Posted 2006-07-06 16:07
初级用户 Posts 14 Credits 36
Indeed, just like what the second floor said, it's too profound. I can't understand it.
Floor4 pengfei Posted 2006-07-26 22:21
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Can the poster specifically explain?
Floor5 voiL Posted 2006-07-27 00:10
中级用户 Posts 189 Credits 384
There is no Chinese explanation, and I can only understand part of it.
Floor6 IceCrack Posted 2006-07-27 00:52
中级用户 Posts 168 Credits 332 From 天涯
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.
Floor7 qwr123 Posted 2006-07-27 11:50
新手上路 Posts 5 Credits 14
Too profound, can't understand it
Floor8 Climbing Posted 2006-07-27 14:50
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
It's pretty good. I really didn't notice before that the more command has so many parameters.
Floor9 namejm Posted 2006-07-27 18:51
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor10 hxuan999 Posted 2006-11-23 03:33
中级用户 Posts 161 Credits 337
A bit messy
Floor11 redtek Posted 2006-11-25 03:19
金牌会员 Posts 1,147 Credits 2,902
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~:)
Floor12 foxfast Posted 2007-01-15 03:51
新手上路 Posts 6 Credits 14
Really confused, the path from ÷ in the signature is wrong
Floor13 xiaohacker Posted 2007-01-15 05:30
初级用户 Posts 45 Credits 110
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!
Floor14 ccwan Posted 2007-01-15 06:30
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
Flood

[ Last edited by ccwan on 2007-1-20 at 09:24 PM ]
Floor15 40szb Posted 2007-01-21 09:55
初级用户 Posts 21 Credits 46 From 西安
Let me explain
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023