|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『楼 主』:
[原创]终于都解决了-显示行数&删除最后一行
使用 LLM 解释/回答一下
对于自己提出的问题,
显示行数: http://www.cn-dos.net/forum/viewthread.php?tid=27919&fpage=1
删除最后一行: http://www.cn-dos.net/forum/viewthread.php?tid=27920&fpage=1
终于都解决了
@echo off
setlocal EnableDelayedExpansion
rem 删除临时文件。
if exist result*.txt del result*.txt
rem 计算行数,考虑了空行。
for /f "delims=" %%i in ('find /v /n "" test.txt') do set /a line+=1
set/a num=line-1
echo test.txt文本的行数是:!num!。
pause>nul
rem 下面是输出去除最后一行后的文本内容,不过用了几个临时文件进行转移。
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 删除不需要的临时文件result0.txt,只保留最后结果。
del result0.txt
各位可以多测试特殊字符,看看结果是不是正确。
谢谢了。
有问题请批评指正。
Last edited by namejm on 2007-2-27 at 02:12 PM ]
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 ]
|
|
2007-2-26 13:02 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
用findstr的/n输出的可以包含空行,你再读一遍……这个你才发的时候就想到了,太费周章了……需要2次就没发,所以最后还是建议你用脚本,读一次就可以了。
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.
|
|
2007-2-26 13:15 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
2楼
用findstr的/n输出的可以包含空行,你再读一遍……
的确可以的啊,你那里不可以么?
需要2次
一个是find 一个是findstr
好不。你可以讲清楚么?还有那里有问题!?
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!?
|
|
2007-2-26 13:22 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
那我就发一个只有一个find的代码,真正只读一次:
@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 文本行数是:!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 ]
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 ]
|
|
2007-2-26 13:23 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>new2.txt) else echo.>>new2.txt)
这段代码是判断只有一个回车符号的行的,
并作处理为, 若是回车换行,就echo.
若不是,就把原文输出。
Last edited by scriptor on 2007-2-26 at 12:31 AM ]
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 ]
|
|
2007-2-26 13:26 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
但是若有一行全是空格构成的,就有问题了,比如:
转换后,输出为
echo 处于关闭状态。
怎么解决?
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?
|
|
2007-2-26 13:37 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
唉,看着麻烦。脚本简单就用脚本写……用最适合的最有效率的代码...个人的原则。。。
硬要p处理文本有点勉强(并不是说做不到)
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)
|
|
2007-2-26 13:42 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
我可不写vbs和js了
你可不可以写一个给我??
I won't write VBS and JS. Can you write one for me??
|
|
2007-2-26 14:01 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Const ForReading = 1
Const ForWriting = 2
Dim strText,strNewText,OutStr
Set objFSO = Createobject("Scripting.Filesystemobject")
Set objFile = objFSO.OpenTextFile("d:\slorelee\桌面\1.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Split(strText,vbCrlf)
Msgbox "有" & Cstr(Ubound(strNewText)+1) & "行"
strNewText(Ubound(strNewText))=Empty
OutStr=Join(strNewText,vbCrlf)
OutStr=Mid(OutStr,1,Len(OutStr)-2)
Set objFile = objFSO.OpenTextFile("d:\slorelee\桌面\2.txt", ForWriting,True)
objFile.Write OutStr
objFile.Close
```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
```
|
|
2007-2-26 14:24 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by scriptor at 2007-2-26 14:01:
我可不写vbs和js了
你可不可以写一个给我??
处理文件还是sed awk好了
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.
|
|
2007-2-26 16:52 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by vkill at 2007-2-26 03:52:
处理文件还是sed awk好了
脚本也不懒的。。。而且非3方嘛
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
|
|
2007-2-27 01:32 |
|
|
scriptor
银牌会员
    
积分 1187
发帖 555
注册 2006-12-21
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by scriptor at 2007-2-26 00:02:
─────────────────── 版务记录 ────────────────────
执行:namejm
原标题:终于都 ...
你怎么老是减我的分!?
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!?
|
|
2007-2-28 02:55 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
我的扣分理由已经在版务记录里写得很清楚了,如果我经常对你扣分,可能的原因是:
1、你经常把标题写得很模糊;
2、你经常灌水。
被我扣分的不只你一个人,很多人都挨了扳子的。如果处理有失偏颇的情况,请到 意见反馈 & 网友交流 版块发帖提出。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-2-28 03:19 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2007-2-27 02:19 PM:
我的扣分理由已经在版务记录里写得很清楚了,如果我经常对你扣分,可能的原因是:
1、你经常把标题写得很模糊;
2、你经常灌水。 ...
:D:D:D:D:D
幸甚
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
|
|
2007-2-28 05:45 |
|
|
everest79
金牌会员
      一叶枝头,万树皆春
积分 2564
发帖 1127
注册 2006-12-25
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
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%
我也写了个
```
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
|
|
2007-2-28 06:20 |
|