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-07-14 23:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to extract lines 3 through 90 from a file? View 1,699 Replies 9
Original Poster Posted 2007-04-27 00:59 ·  中国 河北 廊坊 霸州市 电信
初级用户
Credits 36
Posts 20
Joined 2007-04-19 05:27
19-year member
UID 85695
Gender Male
Status Offline
How can I extract lines 3 through 90 from a file and pass them to a new *.txt file! Thanks!
Floor 2 Posted 2007-04-27 02:03 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
skip 2 lines, read once >new.txt

skip 89 lines, read once >>new.txt
Floor 3 Posted 2007-04-27 02:07 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
I seem to have written one before, but I can't find it.
Floor 4 Posted 2007-04-27 02:15 ·  中国 北京 联通
中级用户
★★
替身
Credits 269
Posts 130
Joined 2007-03-23 21:41
19-year member
UID 82683
Gender Female
Status Offline
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
everest79 +15 2007-04-27 02:17
Floor 5 Posted 2007-04-27 02:24 ·  中国 新疆 乌鲁木齐 天山区 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
Could post #4 explain the code?
Floor 6 Posted 2007-04-27 02:32 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@echo off

for /f "skip=2 delims=" %%i in (1.txt) do (
@echo %%i>>Out.txt
goto Next
)
:Next
for /f "skip=89 delims=" %%i in (1.txt) do (
@echo %%i>>Out.txt
goto Exit
)
:Exit
pause>nul


I still think my own way is better... If there are a lot of lines, the FINDSTR above has to add line numbers to the whole text... and then filter...
Floor 7 Posted 2007-04-27 06:02 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
If what the OP wants is the content from line 3 to line 90, then you can do it like this
@echo off
for /l %%a in (3,1,90) do call :need %%a
:need
for /f "tokens=1* delims=:" %%i in ('findstr /n . 1.txt') do if %%i==%1 echo %%j>>2.txt
Floor 8 Posted 2007-04-27 12:29 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Floor 9 Posted 2007-04-27 23:45 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Output in the original format, cannot output blank lines
@echo off
for /l %%n in (3,1,90) do call :need %%n
pause
exit

:need
set /a line=%1-1
for /f "skip=%line% delims=" %%i in ('more test.txt') do (
echo %%i
goto :eof
)


Can output blank lines, but each line must not contain the “line number:” characters that are the same as the line number, and it cannot output the “!、:” characters or the characters enclosed by “!、:” on the same line
@echo off
setlocal EnableDelayedExpansion
for /l %%n in (31,90) do call :need %%n
pause
exit

:need
set /a line=%1-1
for /f "skip=%line% delims=" %%i in ('findstr /n .* "test.txt"') do (
set "str=%%i"
set "str=!str:%1:=!"
if not "!str!" == "" (echo !str!) else echo.
goto :eof
)


[ Last edited by zh159 on 2007-4-27 at 10:49 AM ]
Floor 10 Posted 2007-04-28 00:38 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Forum Jump: