标题: [文本处理] [已解决]文本内容按行替换,求快捷的批处理 [打印本页]
作者: ttszuo 时间: 2015-7-21 22:48 标题: [已解决]文本内容按行替换,求快捷的批处理
我要修改"文件.txt"的内容,按"要替换的内容.txt"中的行号来替换
要替换的内容.txt的内容为- 8,AOA7AAAQAACkijwAAgAAAAAAQAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAAAAAA
- 5880,i9iDxASJXeyLReyFwHQKxwVkEW0AAQAAAItF7IP4B3cX/ySFFFhEAItNqOhAGwAA
- 5944,/9aFwHQFiV246wTGRecBi0XsUP8VaCBhAIpd54TbdC6NfYCDyf8zwPKu99FJg/kX
- 5950,hfZ1NYsN6BdtAI1V6I2FgP7//1KLVexQagBqAFFSx0XoAAEAAP8VBCBhAPfYG8BA
- 5981,AYrKOtN1DoPAAoPGAoTJddwzwOsFG8CD2P+FwHUJi0XsUOnCAgAAofgXbQCNtYD+
- 6253,XmSJDQAAAACDxBDDkJCQkGr/aBuXXwBkoQAAAABQZIklAAAAAIHsiAEAAKFkxWwA
- 6877,IGEAjU3ojZVM/v//UYtN5FJqAGoAUFHHRegAAQAA/9OLVeSL8PfeG/ZSRv8VaCBh
- 6904,DQAAAABfXluL5V3DiV3g6wSFwHRtagDof3wXAFDoy4oXAIPECOjQihcAiUXs20Xs
- 8090,/xWsJ2EAiJ4EAgAAi4YkAgAAPbiIAAAPjZsAAACLlsgBAAC/AQAAADPJO0oIcyCL
- 8115,BACLVhxqb1L/FawnYQCLRhxqAGhvFAAAaBEBAABQxob8AQAAAP8VuCdhAItN9GSJ
- 9979,RCQkEAAAAP8VBCBhAIXAD4XrAAAAjUQkOFDolUYVAIPEBI1MJBSNVCQcjUQkIFFS
- 10060,jUwkYMaEJAwDAAAG6I8H/f+LRCQcjYwkwAAAAFBR6A0pFQCDxAiFwA+ESAIAALI+
- 10107,AAAAK/CD/jwPhoAAAACLRCQYaglomRQAAGgRAQAAi0gcUf8VuCdhAItUJBRS/xVo
- 10116,YMaEJAwDAAAG6BL9/P+LRCQcjYwkwAAAAFBR6JAeFQCDxAiFwA+ESAIAALI+jXwk
- 10120,dRyEyXQUilgBiss6XgF1DoPAAoPGAoTJdeAzwOsFG8CD2P+FwHUYi1QkGFBomRQA
- 10123,WAGKyzpeAXUOg8ACg8YChMl14DPA6wUbwIPY/4XAdRmLRCQYagdomRQAAGgRAQAA
- 10128,M8DrBRvAg9j/hcB1DFCLRCRAUP8VDCBhAGoAi0wkHGiZFAAAaBEBAACLURxS/xW4
- 15717,AIlN3Il95GaJfeChCBFtAMZF/AE7x4vwdQW+DBFtALp0IGoAhdIPhPsAAAA79w+E
- 16357,0g+EAgEAAIX2D4T6AAAAuCx1agCFwA+E7QAAAFKNjXT////o6CT0/1aNTbTGRfwS
- 21736,dGVT6Jhg8/+FwHVbOX0IdQq4BAAAAOnQBgAAi0UEOXgwdESNTCQk6LSl/f+LTQSJ
- 21818,AF9eW4vlXcONlSj///9qMlJoEwQAAIvL6KcyDgCFwHUOUKFgH20AaIQfagBQ68GN
- 21823,M8DyrvfRSXUSiw1UH20AagBohB9qAOnk/v//jb18////g8n/M8DyrvfRSTiUDXv/
- 21832,i8iD+f90FY0E/wPPRo08gevTQOveikXvhMB0FosNVB9tAGoAaIQfagBRi03I6SD9
- 23263,oXARbQCFwA+E3AEAAKHoD20AhcB0BYlF7OsKx0Xs7A9tAItF7Is9+A9tAIX/dAWJ
- 33363,av9o6NpgAGShAAAAAFBkiSUAAAAAg+w0VleL+Y1MJBToyxcAAIuHqEkAAGgAAQAA
- 57969,JWxkKQoAAAAudGVzdGluZ2V4dAAiJSVzIiAvY2ggJWxkIC93ICVJNjRkAABJAEQA
复制代码
其中","前的为行号而 ","的为替换后的内容
直观的说就是
把"文件.txt"第8行的内容替换为AOA7AAAQAACkijwAAgAAAAAAQAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAAAAAA
把"文件.txt"第5880行的内容替换为i9iDxASJXeyLReyFwHQKxwVkEW0AAQAAAItF7IP4B3cX/ySFFFhEAItNqOhAGwAA
把"文件.txt"第5944行的内容替换为/9aFwHQFiV246wTGRecBi0XsUP8VaCBhAIpd54TbdC6NfYCDyf8zwPKu99FJg/kX
把"文件.txt"第5950行的内容替换为hfZ1NYsN6BdtAI1V6I2FgP7//1KLVexQagBqAFFSx0XoAAEAAP8VBCBhAPfYG8BA
.......
要替换的文件有5M多,自己做了个批处理感觉替换速度太慢,求更快速的批处理或者第三方的批处理方便处理大文件- @echo off
- setlocal enabledelayedexpansion
- set hang=0
- set tiaoguo=0
- call :huoqu
- cd.>处理的后文件.txt
- for /f "delims=" %%a in (文件.txt) do (
- set /a hang=!hang!+1
- if "!hang!"=="!hanghao!" (
- echo.!neirong!>>处理的后文件.txt
- call :huoqu
- ) else (
- echo.%%a>>处理的后文件.txt"
- )
- )
- pause
- exit
-
- :huoqu
- for /f "tokens=1,2 delims=," %%a in ('more +!tiaoguo! 要替换的内容.txt') do (
- set /a tiaoguo=!tiaoguo!+1
- set hanghao=%%a
- set neirong=%%b
- goto :EOF
- )
复制代码
"文件.txt"和"要替换的内容.txt"下载地址
http://share.weiyun.com/c281813f4eecc89e2a71fe64030f1b0e
作者: pcl_test 时间: 2015-7-22 00:44
本帖最后由 pcl_test 于 2015-7-22 00:49 编辑
vbs- Dim fso,File1,File2,File3,objDict
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set File1 = fso.OpenTextFile(".\要替换的内容.txt", 1)
- Set File2 = fso.OpenTextFile(".\文件.txt", 1)
- Set File3 = fso.CreateTextFile(".\处理后的文件.txt", 2 ,False)
- Set objDict = WSH.CreateObject("Scripting.Dictionary")
- Do While File1.AtEndOfStream <> True
- arr = split(File1.ReadLine,",")
- objDict.Add arr(0), arr(1)
- Loop
-
- Do While File2.AtEndOfStream <> True
- n=n+1
- str = File2.ReadLine
- If objDict.Exists(CStr(n)) Then
- File3.WriteLine objDict.Item(CStr(n))
- Else
- File3.WriteLine str
- End If
- Loop
- File1.Close
- File2.Close
- File3.Close
- MsgBox "完成!"
复制代码
作者: pcl_test 时间: 2015-7-22 08:25
- @echo off&setlocal enabledelayedexpansion
- for /f "usebackq tokens=1* delims=," %%a in ("要替换的内容.txt") do set "#%%a=%%b"
- (for /f "usebackq delims=" %%b in ("文件.txt") do (
- set /a n+=1
- if defined #!n! (
- for %%c in (!n!) do echo;!#%%c!
- ) else echo;%%b
- ))>"处理后的文件.txt"
- pause
复制代码
作者: ttszuo 时间: 2015-7-22 09:54
回复 3# tigerpower
这个的处理速度很快,处理后的文件原行的内容并没有删除而是加在替换内容的后面,应该怎么修改才能除去原行的内容
作者: 回家路上 时间: 2015-7-22 16:26
嗯,大牛就是厉害。
我看你的批处理就是复制代码
执行太多次了,这写文件操作特别费时。每行都调一次这个,时间都去哪儿了?=
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |