中国DOS联盟论坛

中国DOS联盟
-- 联合DOS 推动DOS 发展DOS --

联盟域名:cndos.rths.ml  论坛域名:cndos.rths.ml/forum

DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

» 游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助

 
« « 2021-7 » »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

栏目分类

最热的 5 篇文章

搜索网志文章


在线用户 : 13
0 位会员   13 位游客
[资料]如何提取不同行上的内容
中文翻译见18楼(版务 by HAT @ 2009-01-04)

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


  Quote:
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
)


评论
<<   [1] [2]  >>   >



  Quote:
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 . ...

对一我们新鸟来说太难,要是有中文说明就好了.

tclshx 2006-7-5 23:37

的确就像二楼说的那样  太深奥了 看不懂啊

hanbsome 2006-7-6 16:07

楼主能不能具体解释一下.

pengfei 2006-7-26 22:21

没有中文的说明,只能看懂一部分.

voiL 2006-7-27 00:10

英文单词不是太多啊.不认识的可以用百度词典查一下的.而且批处理不会的话.应该把哪个不会说出来.这样也好解释.如果你全篇看不懂的话.那么你不适合看这篇文章.可以看一下我签名中的这个电子书.应该对你有帮助的

IceCrack 2006-7-27 00:52

太深奥了 看不懂啊

qwr123 2006-7-27 11:50

很不错,以前还真没有注意到more命令有那么多的参数。

Climbing 2006-7-27 14:50

  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。

namejm 2006-7-27 18:51

有点乱

hxuan999 2006-11-23 03:33



  Quote:
Originally posted by namejm at 2006-7-27 05:51:
  more的用法真是强。以后要显示指定行的内容就有模式可套用了,呵呵,不错不错。

同感,无限佩服~:)
好玩的贴子不能沉下去~:)

redtek 2006-11-25 03:19

真的很晕,签名里从÷的路径是错了的啊

foxfast 2007-1-15 03:51

佩服的五体投地
强人,我佩服的五体投地,但是没有中文说明,我是看的一塌糊涂!唉,请高手们指点一下我等菜们应该看一些什么样的dos技术书!

xiaohacker 2007-1-15 05:30

灌水

[ Last edited by ccwan on 2007-1-20 at 09:24 PM ]

ccwan 2007-1-15 06:30

说明一下

40szb 2007-1-21 09:55

发表评论
标题:   [可选]
选项:
 禁用 URL 识别
 禁用 Smilies
 禁用 Discuz! 代码
 使用个人签名
 接收新回复邮件通知

           
[完成后可按 Ctrl+Enter 发布]

可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



[ 联系我们 - 中国DOS联盟 ]


Powered by Discuz! © 2001-2011