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-05 20:23
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original]Finally solved - display line number & delete last line View 3,122 Replies 22
Floor 16 Posted 2007-02-28 08:56 ·  中国 四川 成都 电信
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Originally posted by scriptor at 2007-2-28 02:55:


Why do you always deduct my points!?

Find more reasons on your own~~

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 17 Posted 2007-09-15 12:31 ·  中国 北京 中国科学院研究生院玉泉四公寓
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Originally posted by scriptor at 2007-2-26 13:02:
For the problem I raised,
Display line number: http://www.cn-dos.net/forum/viewthread.php?tid=27919&fpage=1

Delete last line: http://www.cn-dos.net/forum/viewthread.php?ti ... Modification of this text! The code written before did not achieve the desired effect! Because, if the last line is a pure carriage return, it still exists when output. This program has been slightly modified on the original basis to achieve the effect! Everyone can test! @echo off setlocal EnableDelayedExpansion rem Delete temporary files. if exist result*.txt del result*.txt rem Calculate the number of lines, considering blank lines. copy test.txt tt.txt echo __>>tt.txt for /f "delims= skip=2" %%i in ('find /v /n "" tt.txt') do set /a line+=1 ::set/a num=line del tt.txt echo The number of lines in test.txt is: !line!. pause>nul rem The following is to output the text content after removing the last line, but several temporary files are used for transfer. for /f "tokens=1,2* delims=" %%i in ('findstr /x /n .* test.txt') do ( set /a l+=1 if "!l!" neq "!line!">>result0.txt echo %%i ) set /a l=line-2 set /a ll=line-1 for /f "tokens=1,* delims=:" %%i in (result0.txt) do ( rem Originally here was 1,2* set /a n+=1 ::if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>result.txt) else echo.>>result.txt) if !n! leq !l! ( if not "%%j"=="" (echo %%j>>result.txt) else (echo.>>result.txt)) if !n! equ !ll! set/p a=%%j>>result.txt<nul ) rem Delete the unnecessary temporary file result0.txt, only keep the final result. del result0.txt Finished! Improve it once more, it should be correct! [ Last edited by scriptor on 2007-9-15 at 01:26 PM ]
Floor 18 Posted 2007-09-15 13:38 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Is this simpler?
@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set "m=!m:*:=!"
if !nm! EQU !mn! goto end
echo.!m!>>test2.txt
endlocal
)
:end
pause
Floor 19 Posted 2007-09-15 14:19 ·  中国 北京 中国科学院研究生院玉泉四公寓
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Tested.There is a problem that certain characters in lines containing! cannot be processed. For example,!#$%@. The! will be omitted in the output because setlocal is in effect!
Floor 20 Posted 2007-09-15 14:20 ·  中国 北京 中国科学院研究生院玉泉四公寓
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Originally posted by terse at 2007-9-15 13:38:
Is this simpler?
@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n . ...



Not bad a
There are problems:
1: The last line has an extra blank line!
2: When there are blank lines in the middle of the text, it cannot be handled well
For example:
1
2

3
....
Your code output is:
1
2
3
.....

And there is an extra blank line at the end

[ Last edited by scriptor on 2007-9-15 at 02:23 PM ]
Floor 21 Posted 2007-09-15 14:24 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Everything here is fine. I don't know how you tried it. Finally, there are indeed extra blank lines, but blank lines can be handled.

The content of test.txt is as follows:

@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set "m=!m:*:=!"

if !nm! EQU !mn! goto end

echo.!m!>>test2.txt

endlocal

)
:end

pause

%^>:::<|^aue||!str!~ ! @ # $ % ^ & * ( () <>nul

set ok=^
!m!

!m!

!m!
!nm!
!mn!
!mn!

[ Last edited by terse on 2007-9-15 at 02:31 PM ]
Floor 22 Posted 2007-09-15 14:33 ·  中国 北京 中国科学院研究生院玉泉四公寓
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Well, it's done.

Yours can output exclamation marks!

I mean mine can't!!!

Hehe....

Your code is still powerful!

But what about the last line?

But still, thanks a lot.

Mine was written too bulky.

Haha....
Floor 23 Posted 2007-09-15 14:45 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by scriptor at 2007-9-15 14:33:
Well, it's done.

Yours can output exclamation marks!
I mean mine can't!!!

Hehe....
Your code is still powerful!
But what about the last line?

But still, thanks a lot.
Mine was written too bulky.
Hahaha....

Thought it didn't work with my above one! Misunderstood! so~!
Forum Jump: