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:19
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original]Finally solved - display line number & delete last line View 3,120 Replies 22
Original Poster Posted 2007-02-26 13:02 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
For the question I raised,

Show line number: http://www.cn-dos.net/forum/viewthread.php?tid=27919&fpage=1

Delete the last line: http://www.cn-dos.net/forum/viewthread.php?tid=27920&fpage=1
Finally all solved


@echo off
setlocal EnableDelayedExpansion

rem Delete temporary files.
if exist result*.txt del result*.txt

rem Calculate the number of lines, considering blank lines.
for /f "delims=" %%i in ('find /v /n "" test.txt') do set /a line+=1
set/a num=line-1
echo The number of lines in test.txt is: !num!.
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 n+=1
if "!n!" neq "!line!">>result0.txt echo %%i
)

for /f "tokens=1,2* delims=:" %%i in (result0.txt) do (
set /a n+=1
if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>result.txt) else echo.>>new2.txt)
)

rem Delete the unnecessary temporary file result0.txt, only keep the final result.
del result0.txt



You can test special characters more, and see if the result is correct.
Thanks.
Please criticize and correct if there are any problems.

[ Last edited by namejm on 2007-2-27 at 02:12 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
namejm -2 2007-02-27 01:52
Floor 2 Posted 2007-02-26 13:15 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
The output with /n from findstr can contain blank lines. You read it again... You thought of this when you first posted it, it's too cumbersome... It wasn't posted because it required 2 times, so finally it's suggested that you use a script, which can be read once.
Floor 3 Posted 2007-02-26 13:22 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Floor 2
The output of findstr with /n can include empty lines, read it again...

Indeed, it can. Doesn't it work for you?
Need twice

One is find and the other is findstr
Okay. Can you make it clear? Where is the problem!?
Floor 4 Posted 2007-02-26 13:23 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Then I'll post a code with only one find, truly reading only once:



@echo off
setlocal EnableDelayedExpansion

if exist new*.txt del new*.txt
for /f "delims=" %%i in ('find /v /n "" old.txt') do set /a line+=1
set/a num=line-1
echo The number of text lines is: !num!.
pause>nul

for /f "tokens=1,2* delims=:" %%i in ('findstr /x /n .* old.txt') do (
set /a n+=1
if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>new2.txt) else echo.>>new2.txt)
)


[ Last edited by scriptor on 2007-2-26 at 12:32 AM ]
Floor 5 Posted 2007-02-26 13:26 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline

if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>new2.txt) else echo.>>new2.txt)


This code is to judge the lines with only one carriage return symbol,
and the processing is: if it is a carriage return and line feed, then echo.,
if not, then output the original text.

[ Last edited by scriptor on 2007-2-26 at 12:31 AM ]
Floor 6 Posted 2007-02-26 13:37 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
But if there is a line consisting entirely of spaces, there is a problem. For example:

After conversion, the output is "echo is in the off state."

How to solve it?
Floor 7 Posted 2007-02-26 13:42 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Alas, it looks troublesome. If the script is simple, use the script to write... use the most suitable and most efficient code... personal principle...

It's a bit far-fetched to force p to process the text (not to say it can't be done)
Floor 8 Posted 2007-02-26 14:01 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
I won't write VBS and JS. Can you write one for me??
Floor 9 Posted 2007-02-26 14:24 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
```vb
Const ForReading = 1
Const ForWriting = 2
Dim strText, strNewText, OutStr
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("d:\slorelee\Desktop\1.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Split(strText, vbCrlf)
MsgBox "There are " & CStr(Ubound(strNewText) + 1) & " lines"

strNewText(Ubound(strNewText)) = Empty

OutStr = Join(strNewText, vbCrlf)
OutStr = Mid(OutStr, 1, Len(OutStr) - 2)

Set objFile = objFSO.OpenTextFile("d:\slorelee\Desktop\2.txt", ForWriting, True)
objFile.Write OutStr
objFile.Close
```
Floor 10 Posted 2007-02-26 16:52 ·  中国 宁夏 银川 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by scriptor at 2007-2-26 14:01:
I won't write VBS and JS anymore.
Can you write one for me??

For file processing, it's better to use sed and awk.
Floor 11 Posted 2007-02-27 01:32 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Originally posted by vkill at 2007-2-26 03:52:

File processing is better with sed and awk


The script is not lazy... and it's non-third-party
Floor 12 Posted 2007-02-28 02:55 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
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 00:02:

─────────────────── Moderation Record ────────────────────
Performed by: namejm
Original Title: Finally all ...


Why do you always deduct my points!?
Floor 13 Posted 2007-02-28 03:19 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
My deduction reasons are clearly stated in the moderation records. If I often deduct points from you, possible reasons are:
1. You often write titles very vaguely;
2. You often post water content.

I'm not the only one who deducts points. Many people have received punishments. If there is any unfair handling, please post in the "Feedback & User Communication" section.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 14 Posted 2007-02-28 05:45 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Originally posted by namejm at 2007-2-27 02:19 PM:
  My reason for deduction has been clearly written in the moderation record. If I often deduct points from you, the possible reasons are:
  1. You often write very vague titles;
  2. You often post water content. ...

:D:D:D:D:D
Fortunately
Floor 15 Posted 2007-02-28 06:20 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
```
for /f "delims=:" %%i in ('findstr /n . readme.txt') do set /a num=%%i-1
for /f "tokens=*" %%i in ('more readme.txt +%num%') do set xxx=%%i
type readme.txt|find /v "%xxx%">readme2.txt&&echo %num%
```
I also wrote one
Forum Jump: