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-08-04 02:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to append the string from a specified line to a file View 856 Replies 2
Original Poster Posted 2007-04-06 05:57 ·  中国 浙江 杭州 电信
初级用户
Credits 39
Posts 15
Joined 2007-04-04 04:40
19-year member
UID 83902
Gender Male
Status Offline
I have a bunch of data text files stored somewhat like this,

Identifier a: 1111 units
There may be N identifiers in between
Identifier b: 222 units
Separator
Identifier c: 333 units
Identifier c: 4444 units
Identifier d: 55 units


Characteristics of the text: identifiers may repeat, the length of each piece of data is not fixed, and the units for each kind may not necessarily be the same.
I want to find the data on the line below Separator in this kind of data text and append it to a file.

My idea is to first determine the line number X of Separator, read line X+1, and extract the needed data. Since I'm a newbie, after reading those posts in the sticky index I'm still kind of confused. The batch file I wrote below is also pretty clumsy, and it still has some problems: for example, if the data length changes, there's no way to record it accurately!


@echo off
setlocal
for /f "tokens=1,* delims=:" %%a in ('findstr /n /c:"分隔符" test.txt') do (
set line=%%a
)
set num=0
setlocal enabledelayedexpansion
for /f "skip=%line% delims=" %%i in (test.txt) do (
set /a num+=1
set var=%%i
if !num! equ 1 echo !var:~7,3!>>num.txt
)
endlocal


I don't know which expert could help point me in the right direction and optimize this a bit.

[ Last edited by 29984365 on 2007-4-6 at 06:12 PM ]
Floor 2 Posted 2007-04-06 07:00 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

Based on the additional requirements and goal the OP mentioned, I wrote this batch file. OP, give it a try; if it doesn't work, then reply and we can discuss it further.

@Echo off&SetLocal EnableDelayedExpansion
for /f "delims=" %%a in ('findstr ">.*<" ko_.html') do (
set "string=%%a"
set "string=!string:>=\!"&set "string=!string:<=\!"
if defined lxmxn (set "lxmxn="&&call :printf "!string!") else (echo !string!|find "总帖数" >nul 2>nul&&set lxmxn=cn-dos)
)
:end
pause&exit/b


:printf
set hello=%~1
for /f "tokens=2 delims=\" %%i in ("%hello%") do echo %%i
exit/b

Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
29984365 +1 2007-04-06 07:12
Floor 3 Posted 2007-04-06 07:12 ·  中国 浙江 杭州 电信
初级用户
Credits 39
Posts 15
Joined 2007-04-04 04:40
19-year member
UID 83902
Gender Male
Status Offline
Tested it, works very well.

If processing a lot of files, I guess it will take a long wait =.=
Forum Jump: