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 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Data]How to Extract Content on Different Lines DigestI View 12,411 Replies 19
Floor 16 Posted 2007-01-24 06:35 ·  中国 广东 茂名 电信
初级用户
Credits 45
Posts 23
Joined 2007-01-24 04:39
19-year member
UID 77496
Gender Male
Status Offline
It's too profound. I can't understand it!
Floor 17 Posted 2007-01-24 06:46 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I'll add another post. I posted the translation on the 14th floor before, but in the following floors, some people said they still couldn't understand. Considering my poor English level might cause misunderstandings, I deleted it. I'm posting it again today and ask experts to correct it.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 18 Posted 2007-01-24 06:46 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Don't the friends who understand English see it?
It seems I have to try with my poor level, hoping to start a discussion.
Please criticize and correct but don't laugh, my skin is very thin. ^_^
This page tells everyone how to read a certain line from a text file. There are many methods to use for/f to read the content of input.txt, for example:

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) can get consistent results in different operating systems such as Windows NT, 2000, XP and 2003. The first method cannot recognize UNICODE-encoded files, and if the input file name contains spaces, the usebackq parameter must be used. The second method, using the type command, also does not recognize UNICODE files in Windows 2000, XP and 2003 if the file does not start with a byte order mark (BOM).

In all examples, assume the content of numbers.txt is:

one
two
three
four
five
six
seven
eight
nine
ten

Display the first line

This example outputs 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%

Display the first few lines

This example outputs 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
)

Display the last line

This example outputs ten.

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

Display the last few lines

This example outputs 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
)

Display the nth line

This example outputs three. Note that the /e parameter of more is used here to skip the specified number of lines, and when used with for/f, it will fail if the value is less than 1.


@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%

Display the nth line plus X lines

This example outputs 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
htysm +1 2007-01-24 07:53
namejm +4 2007-02-06 01:30
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 19 Posted 2007-03-21 23:11 ·  中国 浙江 宁波 电信
初级用户
Credits 62
Posts 24
Joined 2007-03-17 09:34
19-year member
UID 82057
Gender Male
Status Offline
Encountering lines like this is ineffective??

<td width=45 align=center rowspan=2>3</td>
<td width=83 align=center rowspan=2>02.I</td> <!-- @T -->
Floor 20 Posted 2007-07-29 09:20 ·  中国 北京 海淀区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
Now you can use bat to do RSS.
Forum Jump: