标题: [文本处理] [已解决]批处理如何把文本内容按指定条件排序? [打印本页]
作者: czvde 时间: 2022-3-31 23:20 标题: [已解决]批处理如何把文本内容按指定条件排序?
a.txt
例如:a.txt- ----------
- 123456
- bbjdik
- ----------
- 1210000
- bbjdi
复制代码
排序,输出a-1.txt
a-1.txt- ----------
- 1210000
- bbjdi
- ----------
- 123456
- bbjdik
复制代码
已解决!
作者: buyiyang 时间: 2022-4-1 17:06
本帖最后由 buyiyang 于 2022-4-2 00:55 编辑
- @echo off
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
- for %%i in (*.txt) do (
- for /f "delims=" %%a in ('type "%%i"') do (set /p=%%a#<nul>>by.txt)
- for /f "delims=" %%b in (by.txt) do set "str=%%b"&&set "str=!str:----------#==!"
- for %%c in (!str!) do echo %%c>>byy.txt
- sort byy.txt >by.txt
- cd.>byy.txt
- for /f "delims=" %%x in ('type by.txt') do echo ----------#%%x>>byy.txt
- cd.>by.txt
- for /f "delims=" %%y in (byy.txt) do (
- set "strr=%%y"&&set "strr=!strr:#==!"
- for %%z in (!strr!) do >> by.txt echo,%%z
- )
- copy /y by.txt "%%~ni-1.txt"
- echo %%~ni-1.txt
- del /q by.txt
- del /q byy.txt
- )
- echo 按任意键结束&pause>nul
复制代码
水平有限,用的笨办法。希望你的目录下没有by.txt,byy.txt文件。
作者: went 时间: 2022-4-1 21:05
test.bat- @echo off & cd /d "%~dp0"
- setlocal enabledelayedexpansion
- set "s=----------"
- for %%i in (*.txt) do (
- for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%%i"') do (
- if not "%%b"=="%s%" (
- if "!a!"=="1" (
- set "b=%%b"
- set /a "a=0"
- ) else call set "_!b!=%%_!b!%%;"%%b";"
- ) else set /a "a=1"
- )
- (
- for /f "tokens=1,2* delims==_" %%a in ('set _') do (
- echo %s%
- echo %%a
- set "_%%a="
- for %%d in (%%b) do echo.%%~d
- )
- ) > "%%~ni-1.txt"
- echo %%~ni-1.txt
- )
- pause&exit
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |