Board logo

标题: [文本处理] [已解决]批处理如何把文本内容按指定条件排序? [打印本页]

作者: czvde    时间: 2022-3-31 23:20     标题: [已解决]批处理如何把文本内容按指定条件排序?

a.txt

例如:a.txt
  1. ----------
  2. 123456
  3. bbjdik
  4. ----------
  5. 1210000
  6. bbjdi
复制代码
排序,输出a-1.txt

a-1.txt
  1. ----------
  2. 1210000
  3. bbjdi
  4. ----------
  5. 123456
  6. bbjdik
复制代码
已解决!
作者: buyiyang    时间: 2022-4-1 17:06

本帖最后由 buyiyang 于 2022-4-2 00:55 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. for %%i in (*.txt) do (
  5. for /f "delims=" %%a in ('type "%%i"') do (set /p=%%a#<nul>>by.txt)
  6. for /f "delims=" %%b in (by.txt) do set "str=%%b"&&set "str=!str:----------#==!"
  7. for %%c in (!str!) do echo %%c>>byy.txt
  8. sort byy.txt >by.txt
  9. cd.>byy.txt
  10. for /f "delims=" %%x in ('type by.txt') do echo ----------#%%x>>byy.txt
  11. cd.>by.txt
  12. for /f "delims=" %%y in (byy.txt) do (
  13. set "strr=%%y"&&set "strr=!strr:#==!"
  14. for %%z in (!strr!) do >> by.txt echo,%%z
  15. )
  16. copy /y by.txt "%%~ni-1.txt"
  17. echo %%~ni-1.txt
  18. del /q by.txt
  19. del /q byy.txt
  20. )
  21. echo 按任意键结束&pause>nul
复制代码
水平有限,用的笨办法。希望你的目录下没有by.txt,byy.txt文件。
作者: went    时间: 2022-4-1 21:05

test.bat
  1. @echo off & cd /d "%~dp0"
  2. setlocal enabledelayedexpansion
  3. set "s=----------"
  4. for %%i in (*.txt) do (
  5. for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%%i"') do (
  6. if not "%%b"=="%s%" (
  7. if "!a!"=="1" (
  8. set "b=%%b"
  9. set /a "a=0"
  10. ) else call set "_!b!=%%_!b!%%;"%%b";"
  11. ) else set /a "a=1"
  12. )
  13. (
  14. for /f "tokens=1,2* delims==_" %%a in ('set _') do (
  15. echo %s%
  16. echo %%a
  17. set "_%%a="
  18. for %%d in (%%b) do echo.%%~d
  19. )
  20. ) > "%%~ni-1.txt"
  21. echo %%~ni-1.txt
  22. )
  23. pause&exit
复制代码





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