Board logo

标题: [文本处理] [已解决]批处理怎样删除连续空行? [打印本页]

作者: honghong33    时间: 2015-2-25 17:36     标题: [已解决]批处理怎样删除连续空行?

本帖最后由 honghong33 于 2015-3-14 20:58 编辑

有一个文件
1



2

3


4

5

6
变成
1

2
3

4
5
6
当只有一个空行是删除空行,空行数大于一时,保留一个空行。
作者: pcl_test    时间: 2015-2-25 22:30

本帖最后由 pcl_test 于 2015-2-25 22:59 编辑

回复 1# honghong33
假设有test.txt
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set n=0
  4. for /f "tokens=1* delims=:" %%a in ('findstr /n .* "test.txt"') do (
  5. set "str=%%b"
  6. if !n! lss 2 if not "!str!" == "" (
  7. >>$ echo,!str!
  8. set n=0
  9. )
  10. if !n! gtr 1 if not "!str!" == "" (
  11. >>$ echo.
  12. >>$ echo,!str!
  13. set n=0
  14. )
  15. if not "!n!" == "0" if "!str!" == "" (
  16. set /a n+=1
  17. )
  18. if "!n!" == "0" if "!str!" == "" (
  19. set /a n+=1
  20. )
  21. )
  22. move $ "test.txt"
  23. pause
复制代码

作者: 慕夜蓝化    时间: 2015-2-26 12:00

本帖最后由 慕夜蓝化 于 2015-2-26 15:29 编辑
  1. @echo off&SETLOCAL ENABLEDELAYEDEXPANSION
  2. (FOR /f "tokens=1* delims=[]" %%i in ('type "a.txt"^|find /v /n ""') do (
  3.     if not "!str!"=="" (
  4.         if not "%%j"=="" (
  5.             echo,%%j)
  6.     ) else ( echo,%%j
  7.     )
  8.     set "str=%%j"
  9. )) >$
  10. more /s $ >b.txt&del /q $
复制代码

作者: CrLf    时间: 2015-2-26 13:55

回复 3# 慕夜蓝化


    用 tokens=1* 更通用一点
作者: 慕夜蓝化    时间: 2015-2-26 15:28

回复 4# CrLf


    嗯嗯,已经更改。
作者: DAIC    时间: 2015-2-26 20:44

如果只需要删除连续空行,不需要删除原有的单个空行,会比较简单:
  1. sed -i "/./,/^$/!d" 1.txt
复制代码

作者: honghong33    时间: 2015-3-6 22:44

回复 3# 慕夜蓝化


    非常感谢
作者: honghong33    时间: 2015-3-6 22:45

回复 2# pcl_test


    非常感谢
作者: honghong33    时间: 2015-3-6 22:45

回复 6# DAIC


    非常感谢
作者: honghong33    时间: 2015-3-6 22:45

回复 4# CrLf


    非常感谢




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2