|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『楼 主』:
不用sed怎么删除空行
使用 LLM 解释/回答一下
sed "/^$/d" file可删除txt文件中的空行,但是不能删除由空格组成的空行,如果某一行上只有几个空格,前面的命令是不能匹配该行的.
请问不用sed怎么删除空行?
最好连有空格的空行也删除的.
Last edited by wert123 on 2007-8-29 at 09:18 AM ]
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.
|
|
2007-8-27 13:38 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
@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
文本中有"的话就要少东西了。。。
@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..."
|
|
2007-8-27 14:14 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
用sed是可以删除的.正则要用好点
Using sed can delete. Regular expressions should be used well
|
|
2007-8-27 14:33 |
|
|
halulove
初级用户
 
积分 43
发帖 19
注册 2007-8-19
状态 离线
|
|
2007-8-27 17:50 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
woshenmodoumeishuocolor]
Last edited by ccwan on 2007-8-27 at 06:42 PM ]
woshenmodoumeishuocolor]
Last edited by ccwan on 2007-8-27 at 06:42 PM ]
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-8-27 18:38 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
恩。不过看都看晕了。应该很方便的。。
Hmm. But it's all confusing to look at. It should be quite convenient, though.
|
|
2007-8-27 18:42 |
|
|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
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 ...
谢谢,knoppix7真厉害啊
文本中有"的话就怎么会要少东西呢?能改进吗
顺便问下,echo+是什么意思啊.除此外其它地方
几乎也看不懂,麻烦帮忙讲解下吧.
Last edited by wert123 on 2007-8-28 at 06:36 PM ]
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 ]
|
|
2007-8-28 18:23 |
|
|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by wudixin96 at 2007-8-27 02:33 PM:
用sed是可以删除的.正则要用好点
wudixin96老兄你也帮下忙吧,我在打东西,
打印室里面没有sed,我也想用,要下载,打印员
又怕sed是毒,打出来都有很多echo.一样的东西,乱费读书钱啊.
Last edited by wert123 on 2007-8-28 at 06:35 PM ]
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 ]
|
|
2007-8-28 18:30 |
|
|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
p不行的话,谁帮忙写个vbs的也可以啊
If p doesn't work, who can help write a VBS one as well?
|
|
2007-8-28 18:31 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
你先把"替换成其他得东西(要求在文章中没有出现)
等转换完了再替换回去。
Replace something with something else (required not to have appeared in the article) and then replace it back after the conversion.
|
|
2007-8-28 19:12 |
|
|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
knoppix7大虾能把这个P讲下吗,func1我几乎都看不懂
Diners who are knowledgeable about Knoppix 7, can you explain this P? I can hardly understand func1
|
|
2007-8-28 20:13 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
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:文本最后会产生个回车,我没有处理。。。自己取下
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
|
|
2007-8-28 20:28 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
自我感觉没什么好讲得。很简单得BAT.......
I don't think there's much to talk about. It's a very simple BAT...
|
|
2007-8-28 20:30 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
|
2007-8-29 10:05 |
|
|
wert123
中级用户
  
积分 301
发帖 135
注册 2007-5-15
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
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 ...
能不能改进下啊,大虾.我昨天用的时候把txt里面的,
非空行前面的空格都弄掉了啊
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.
|
|
2007-8-30 17:16 |
|