中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-06 23:54
48,040 topics / 350,125 posts / today 1 new / 48,252 members
DOS批处理 & 脚本技术(批处理室) » How to delete empty lines without using sed
Printable Version  2,211 / 18
Floor1 wert123 Posted 2007-08-27 13:38
中级用户 Posts 135 Credits 301
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.
Floor2 knoppix7 Posted 2007-08-27 14:14
银牌会员 Posts 634 Credits 1,287 From cmd.exe
@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..."
Floor3 wudixin96 Posted 2007-08-27 14:33
银牌会员 Posts 931 Credits 1,928
Using sed can delete. Regular expressions should be used well
Floor4 halulove Posted 2007-08-27 17:50
初级用户 Posts 19 Credits 43
findstr . file1>file2
Floor5 ccwan Posted 2007-08-27 18:38
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
woshenmodoumeishuocolor]

[ Last edited by ccwan on 2007-8-27 at 06:42 PM ]
Floor6 knoppix7 Posted 2007-08-27 18:42
银牌会员 Posts 634 Credits 1,287 From cmd.exe
Hmm. But it's all confusing to look at. It should be quite convenient, though.
Floor7 wert123 Posted 2007-08-28 18:23
中级用户 Posts 135 Credits 301
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 ]
Floor8 wert123 Posted 2007-08-28 18:30
中级用户 Posts 135 Credits 301
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 ]
Floor9 wert123 Posted 2007-08-28 18:31
中级用户 Posts 135 Credits 301
If p doesn't work, who can help write a VBS one as well?
Floor10 knoppix7 Posted 2007-08-28 19:12
银牌会员 Posts 634 Credits 1,287 From cmd.exe
Replace something with something else (required not to have appeared in the article) and then replace it back after the conversion.
Floor11 wert123 Posted 2007-08-28 20:13
中级用户 Posts 135 Credits 301
Diners who are knowledgeable about Knoppix 7, can you explain this P? I can hardly understand func1
Floor12 slore Posted 2007-08-28 20:28
铂金会员 Posts 2,478 Credits 5,212

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
Floor13 knoppix7 Posted 2007-08-28 20:30
银牌会员 Posts 634 Credits 1,287 From cmd.exe
I don't think there's much to talk about. It's a very simple BAT...
Floor14 wudixin96 Posted 2007-08-29 10:05
银牌会员 Posts 931 Credits 1,928
Floor15 wert123 Posted 2007-08-30 17:16
中级用户 Posts 135 Credits 301
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023