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-06-22 11:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to delete empty lines without using sed View 2,077 Replies 18
Original Poster Posted 2007-08-27 13:38 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
To delete blank lines without using sed, you can use the following method in Python. Here is a simple example:

```python
with open('file', 'r') as f:
lines =
with open('file', 'w') as f:
f.write('\n'.join(lines))
```

This code reads the file, strips whitespace from each line, and only keeps lines that are not empty after stripping, thus removing both empty lines and lines consisting of only spaces. You can adjust the file name as needed.
Floor 2 Posted 2007-08-27 14:14 ·  中国 北京 海淀区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
@echo off
set runtimes=0
FOR /F "delims=" %%i in (myfile.txt) do (
call :func1 "%%i"
)
pause
pause
pause
:func1
set /a runtimes=%runtimes%+1
set EL=1
set T1=%1
set T1=%T1: =%
if NOT %T1%=="" set EL=0
if %EL%==0 (
if NOT %runtimes%==1 echo+>>done.txt
set /p null=%1<nul>>done.txt
)
goto :EOF

There is "If so, there will be less things..."
Floor 3 Posted 2007-08-27 14:33 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Using sed can delete. Regular expressions should be used well
Floor 4 Posted 2007-08-27 17:50 ·  日本 FUJITSU公司
初级用户
Credits 43
Posts 19
Joined 2007-08-19 08:47
18-year member
UID 95320
Gender Male
Status Offline
findstr . file1>file2
Floor 5 Posted 2007-08-27 18:38 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
woshenmodoumeishuocolor]

[ Last edited by ccwan on 2007-8-27 at 06:42 PM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2007-08-27 18:42 ·  中国 北京 海淀区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
Hmm. But it's all confusing to look at. It should be quite convenient, though.
Floor 7 Posted 2007-08-28 18:23 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
Originally posted by knoppix7 at 2007-8-27 02:14 PM:
@echo off
set runtimes=0
FOR /F "delims=" %%i in (myfile.txt) do (
call :func1 "%%i"
)
pause
pause
pause
:func1
set /a runtimes=%runtimes%+1
set EL=1
set T1=%1
set T ...


Thanks, knoppix7 is really amazing
If there is "then how could there be less things? Can it be improved?
By the way, what does echo+ mean? Other places are almost看不懂, please help explain it.

[ Last edited by wert123 on 2007-8-28 at 06:36 PM ]
Floor 8 Posted 2007-08-28 18:30 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
Originally posted by wudixin96 at 2007-8-27 02:33 PM:
Using sed can delete. Regular expressions need to be used well


Brother wudixin96, can you also help me? I'm typing something,
There's no sed in the print room, I also want to use it, need to download, the printer operator
is afraid that sed is a virus, and there are many echo printed out. The same thing, wasting study money ah.

[ Last edited by wert123 on 2007-8-28 at 06:35 PM ]
Floor 9 Posted 2007-08-28 18:31 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
If p doesn't work, who can help write a VBS one as well?
Floor 10 Posted 2007-08-28 19:12 ·  中国 北京 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
Replace something with something else (required not to have appeared in the article) and then replace it back after the conversion.
Floor 11 Posted 2007-08-28 20:13 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
Diners who are knowledgeable about Knoppix 7, can you explain this P? I can hardly understand func1
Floor 12 Posted 2007-08-28 20:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline

Dim objFSO,objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Test.txt",1)
Dim StrLine,OutStr
Do Until objFile.AtEndOfStream
StrLine = objFile.ReadLine
If Replace
(StrLine," ","") <> "" Then OutStr = OutStr & StrLine & vbCrLf
Loop
objFile.Close
Set
objFile = objFSO.OpenTextFile("Out.txt",2,1)
objFile.Write OutStr
objFile.Close
Set
objFile = Nothing
Set
objFSO = Nothing


PS: There will be a carriage return at the end of the text, I didn't handle it... Just remove it yourself
Floor 13 Posted 2007-08-28 20:30 ·  中国 北京 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
I don't think there's much to talk about. It's a very simple BAT...
Floor 14 Posted 2007-08-29 10:05 ·  中国 天津 电信
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Floor 15 Posted 2007-08-30 17:16 ·  中国 湖北 仙桃 电信
中级用户
★★
Credits 301
Posts 135
Joined 2007-05-15 16:49
19-year member
UID 88615
Gender Male
Status Offline
Originally posted by knoppix7 at 2007-8-27 02:14 PM:
@echo off
set runtimes=0
FOR /F "delims=" %%i in (myfile.txt) do (
call :func1 "%%i"
)
pause
pause
pause
:func1
set /a runtimes=%runtimes%+1
set EL=1
set T1=%1
set T ...

Can you improve it, sir. I removed the spaces in front of non - empty lines in the txt when I used it yesterday.
Forum Jump: