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-03 10:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Extract the content of a specified line and ignore everything else View 2,514 Replies 10
Original Poster Posted 2006-04-25 19:37 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Hehe, I searched hard all afternoon and still couldn't find a suitable one, so I hope everyone can help me.
Floor 2 Posted 2006-04-25 19:53 ·  中国 广东 清远 联通
初级用户
★★
Credits 183
Posts 82
Joined 2006-03-28 21:18
20-year member
UID 52959
Status Offline
lmod seems like it can.
Floor 3 Posted 2006-04-25 20:51 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Hehe, I don't really want to rely on other software to do it.
I just want to use batch files~~ Hehe
Floor 4 Posted 2006-04-25 21:04 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Look through it carefully; this was posted before.
Method 1:

for /f "skip=3 delims=" %i in (a.txt) do echo %i

This can display the content starting from the fourth line. If you only want to display the fourth line, you can append &call or &exit at the end.
Method 2:

for /f "delims=: tokens=1,2" %i in ('findstr /n . a.txt') do if "%i"="4" set %i=%j
echo The content of the fourth line is:%4%

Analyze the specific situation specifically.
Floor 5 Posted 2006-04-25 22:28 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Thanks for your answer.
Hehe, I already looked through all those, and I also wrote one myself.
It's just that the efficiency isn't very high (that's how it feels to me),
because every file that needs to be analyzed is over 50MB, and I want to be lazy too~~
What I'm thinking is whether it can directly read the second line to save time.

Suddenly I got a new idea. After reading your reply, I was thinking: we use for to search for a certain string, and for needs to traverse, but my string only needs to be found once and that's OK. Can some other command be used, or can it exit the for statement after finding it? My thoughts are a bit messy for now~~~ Please advise.

[ Last edited by kcdsw on 2006-4-25 at 22:33 ]
Floor 6 Posted 2006-04-25 22:42 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Read findstr /? a few more times; I hope you can figure out the method yourself.
The reason I gave the first plan is exactly because the first one can skip the first three lines and then end. Just looking at other people's stuff won't improve you much, so think more yourself.
Both methods can improve efficiency with only slight modifications.
Oh right, pay attention to the {"regular expressions" section} + {the "/n" parameter section} in findstr. If you understand these two parts, you can easily hit whatever target you point at.
Floor 7 Posted 2006-04-25 22:48 ·  中国 广东 清远 联通
初级用户
★★
Credits 183
Posts 82
Joined 2006-03-28 21:18
20-year member
UID 52959
Status Offline
Dos doesn't support for /f, does it?
Floor 8 Posted 2006-04-25 22:48 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
I got confused.

@echo off
echo Test environment CMD@WinXP sp2, if you use it on other systems please test first!
echo String searching is needed during execution, please do not modify the related files
echo.
echo Running... please wait patiently

echo ----------------------------Execution info----------------------------
For /f %%a in ('dir /b *.ps') do call :name "%%a"
echo ----------------------------------------------------------------
echo Execution complete, press any key to exit
pause >nul
exit

:name
set name=%~n1
if not == goto :eof
for /f "tokens=2 delims=. " %%i in ('findstr pdn.pdf$ %name%.ps') do set file=%%i.xml
set xmlpath="%appdata%\NewspaperDirect\PressReader\queue\%file%"
for /F "tokens=*" %%c in ('type %xmlpath% ^|find "<title>"') do set paper="%%c"
set paper=%paper:~8,-9%
set paper=%paper:&#x20;= %
set name=%paper% %name:~0,4%-%name:~4,2%-%name:~6,2%
rename %1 "%name%.ps" &&echo %~n1.ps renamed to %name%.ps
goto :eof


ok, going to sleep~~ thank you~~

[ Last edited by kcdsw on 2006-4-26 at 14:02 ]
Floor 9 Posted 2006-04-26 00:12 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
I'll change the rest tomorrow. The second time it has to query inside the xml, but since the file is small, it doesn't feel like it affects the speed.

At first I used type redirection and used find to search.
Later, when testing findstr, I discovered its pattern and the speed improved greatly. It can finish in 1 second; before that it had to be measured in minutes.
Alright, work will be a lot easier from now on.

[ Last edited by kcdsw on 2006-4-26 at 00:14 ]
Attachments
新建 BMP 图像 (2).JPG
Floor 10 Posted 2006-04-26 00:16 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
If you're after efficiency, you can try sed

sed -n "2{p;q}" file.txt

It can display the second line of the file.
sed download:http://www.student.northpark.edu/pemente/sed/gsed407x.zip
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 11 Posted 2006-04-26 15:41 ·  中国 北京 鹏博士BGP
中级用户
★★
Credits 404
Posts 179
Joined 2006-03-30 14:44
20-year member
UID 53056
Status Offline
Found a bug

Correction


@echo off
echo Test environment CMD@WinXP sp2, if you use it on other systems please test first!
echo String searching is needed during execution, please do not modify the related files
echo.
echo Running... please wait patiently

echo ----------------------------Execution info----------------------------
For /f %%a in ('dir /b 20*.ps') do call :name "%%a"
echo ----------------------------------------------------------------
echo Execution complete, press any key to exit
pause >nul
exit

:name
set name=%~n1
if not == goto :eof
for /f "tokens=2 delims=. " %%i in ('findstr pdn.pdf$ %name%.ps') do set file=%%i.xml
set xmlpath="%appdata%\NewspaperDirect\PressReader\queue\%file%"
for /F "tokens=*" %%c in ('type %xmlpath% ^|find "<title>"') do set paper="%%c"
set paper=%paper:&#x20;= %
set paper=%paper:~8,-9%
set name=%paper% %name:~0,4%-%name:~4,2%-%name:~6,2%
rename %1 "%name%.ps" &&echo %~n1.ps renamed to %name%.ps
goto :eof


The idea is to use the $ in findstr to delimit the line that needs to be searched at the end.
Because the characters I need only appear on the second line, the efficiency is still acceptable.
But when using the same command to search in an xml file, it went wrong.
In the end I still used type redirection. Fortunately the xml files are not over 20kb, so that's how it is.

I made some simple optimizations to the program. Let's leave it like this for now. Thanks to Wunaihe in the previous post and the brothers above him too~~

[ Last edited by kcdsw on 2006-4-26 at 15:46 ]
Forum Jump: