Board logo

标题: [文本处理] [已解决]文本内容按行替换,求快捷的批处理 [打印本页]

作者: ttszuo    时间: 2015-7-21 22:48     标题: [已解决]文本内容按行替换,求快捷的批处理

我要修改"文件.txt"的内容,按"要替换的内容.txt"中的行号来替换
要替换的内容.txt的内容为
  1. 8,AOA7AAAQAACkijwAAgAAAAAAQAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAAAAAA
  2. 5880,i9iDxASJXeyLReyFwHQKxwVkEW0AAQAAAItF7IP4B3cX/ySFFFhEAItNqOhAGwAA
  3. 5944,/9aFwHQFiV246wTGRecBi0XsUP8VaCBhAIpd54TbdC6NfYCDyf8zwPKu99FJg/kX
  4. 5950,hfZ1NYsN6BdtAI1V6I2FgP7//1KLVexQagBqAFFSx0XoAAEAAP8VBCBhAPfYG8BA
  5. 5981,AYrKOtN1DoPAAoPGAoTJddwzwOsFG8CD2P+FwHUJi0XsUOnCAgAAofgXbQCNtYD+
  6. 6253,XmSJDQAAAACDxBDDkJCQkGr/aBuXXwBkoQAAAABQZIklAAAAAIHsiAEAAKFkxWwA
  7. 6877,IGEAjU3ojZVM/v//UYtN5FJqAGoAUFHHRegAAQAA/9OLVeSL8PfeG/ZSRv8VaCBh
  8. 6904,DQAAAABfXluL5V3DiV3g6wSFwHRtagDof3wXAFDoy4oXAIPECOjQihcAiUXs20Xs
  9. 8090,/xWsJ2EAiJ4EAgAAi4YkAgAAPbiIAAAPjZsAAACLlsgBAAC/AQAAADPJO0oIcyCL
  10. 8115,BACLVhxqb1L/FawnYQCLRhxqAGhvFAAAaBEBAABQxob8AQAAAP8VuCdhAItN9GSJ
  11. 9979,RCQkEAAAAP8VBCBhAIXAD4XrAAAAjUQkOFDolUYVAIPEBI1MJBSNVCQcjUQkIFFS
  12. 10060,jUwkYMaEJAwDAAAG6I8H/f+LRCQcjYwkwAAAAFBR6A0pFQCDxAiFwA+ESAIAALI+
  13. 10107,AAAAK/CD/jwPhoAAAACLRCQYaglomRQAAGgRAQAAi0gcUf8VuCdhAItUJBRS/xVo
  14. 10116,YMaEJAwDAAAG6BL9/P+LRCQcjYwkwAAAAFBR6JAeFQCDxAiFwA+ESAIAALI+jXwk
  15. 10120,dRyEyXQUilgBiss6XgF1DoPAAoPGAoTJdeAzwOsFG8CD2P+FwHUYi1QkGFBomRQA
  16. 10123,WAGKyzpeAXUOg8ACg8YChMl14DPA6wUbwIPY/4XAdRmLRCQYagdomRQAAGgRAQAA
  17. 10128,M8DrBRvAg9j/hcB1DFCLRCRAUP8VDCBhAGoAi0wkHGiZFAAAaBEBAACLURxS/xW4
  18. 15717,AIlN3Il95GaJfeChCBFtAMZF/AE7x4vwdQW+DBFtALp0IGoAhdIPhPsAAAA79w+E
  19. 16357,0g+EAgEAAIX2D4T6AAAAuCx1agCFwA+E7QAAAFKNjXT////o6CT0/1aNTbTGRfwS
  20. 21736,dGVT6Jhg8/+FwHVbOX0IdQq4BAAAAOnQBgAAi0UEOXgwdESNTCQk6LSl/f+LTQSJ
  21. 21818,AF9eW4vlXcONlSj///9qMlJoEwQAAIvL6KcyDgCFwHUOUKFgH20AaIQfagBQ68GN
  22. 21823,M8DyrvfRSXUSiw1UH20AagBohB9qAOnk/v//jb18////g8n/M8DyrvfRSTiUDXv/
  23. 21832,i8iD+f90FY0E/wPPRo08gevTQOveikXvhMB0FosNVB9tAGoAaIQfagBRi03I6SD9
  24. 23263,oXARbQCFwA+E3AEAAKHoD20AhcB0BYlF7OsKx0Xs7A9tAItF7Is9+A9tAIX/dAWJ
  25. 33363,av9o6NpgAGShAAAAAFBkiSUAAAAAg+w0VleL+Y1MJBToyxcAAIuHqEkAAGgAAQAA
  26. 57969,JWxkKQoAAAAudGVzdGluZ2V4dAAiJSVzIiAvY2ggJWxkIC93ICVJNjRkAABJAEQA
复制代码
其中","前的为行号而 ","的为替换后的内容
直观的说就是
把"文件.txt"第8行的内容替换为AOA7AAAQAACkijwAAgAAAAAAQAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAAAAAA
把"文件.txt"第5880行的内容替换为i9iDxASJXeyLReyFwHQKxwVkEW0AAQAAAItF7IP4B3cX/ySFFFhEAItNqOhAGwAA
把"文件.txt"第5944行的内容替换为/9aFwHQFiV246wTGRecBi0XsUP8VaCBhAIpd54TbdC6NfYCDyf8zwPKu99FJg/kX
把"文件.txt"第5950行的内容替换为hfZ1NYsN6BdtAI1V6I2FgP7//1KLVexQagBqAFFSx0XoAAEAAP8VBCBhAPfYG8BA
.......

要替换的文件有5M多,自己做了个批处理感觉替换速度太慢,求更快速的批处理或者第三方的批处理方便处理大文件
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set hang=0
  4. set tiaoguo=0
  5. call :huoqu
  6. cd.>处理的后文件.txt
  7. for /f "delims=" %%a in (文件.txt) do (
  8.     set /a hang=!hang!+1
  9.     if "!hang!"=="!hanghao!" (
  10.         echo.!neirong!>>处理的后文件.txt
  11.         call :huoqu
  12.     ) else (
  13.         echo.%%a>>处理的后文件.txt"
  14.     )
  15. )
  16. pause
  17. exit
  18. :huoqu
  19. for /f "tokens=1,2 delims=," %%a in ('more +!tiaoguo! 要替换的内容.txt') do (
  20.      set /a tiaoguo=!tiaoguo!+1
  21.     set hanghao=%%a
  22.     set neirong=%%b
  23.     goto :EOF
  24. )
复制代码
"文件.txt"和"要替换的内容.txt"下载地址
http://share.weiyun.com/c281813f4eecc89e2a71fe64030f1b0e
作者: pcl_test    时间: 2015-7-22 00:44

本帖最后由 pcl_test 于 2015-7-22 00:49 编辑

vbs
  1. Dim fso,File1,File2,File3,objDict
  2. Set fso = CreateObject("Scripting.FileSystemObject")
  3. Set File1 = fso.OpenTextFile(".\要替换的内容.txt", 1)
  4. Set File2 = fso.OpenTextFile(".\文件.txt", 1)
  5. Set File3 = fso.CreateTextFile(".\处理后的文件.txt", 2 ,False)
  6. Set objDict = WSH.CreateObject("Scripting.Dictionary")
  7. Do While File1.AtEndOfStream <> True
  8.     arr = split(File1.ReadLine,",")
  9.     objDict.Add arr(0), arr(1)
  10. Loop
  11. Do While File2.AtEndOfStream <> True
  12.     n=n+1
  13.     str = File2.ReadLine
  14.     If objDict.Exists(CStr(n)) Then
  15.         File3.WriteLine objDict.Item(CStr(n))
  16.     Else
  17.         File3.WriteLine str
  18.     End If
  19. Loop
  20. File1.Close
  21. File2.Close
  22. File3.Close
  23. MsgBox "完成!"
复制代码

作者: pcl_test    时间: 2015-7-22 08:25

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "usebackq tokens=1* delims=," %%a in ("要替换的内容.txt") do set "#%%a=%%b"
  3. (for /f "usebackq delims=" %%b in ("文件.txt") do (
  4.     set /a n+=1
  5.     if defined #!n! (
  6.          for %%c in (!n!) do echo;!#%%c!
  7.     ) else echo;%%b
  8. ))>"处理后的文件.txt"
  9. pause
复制代码

作者: ttszuo    时间: 2015-7-22 09:54

回复 3# tigerpower
这个的处理速度很快,处理后的文件原行的内容并没有删除而是加在替换内容的后面,应该怎么修改才能除去原行的内容
作者: 回家路上    时间: 2015-7-22 16:26

嗯,大牛就是厉害。

我看你的批处理就是
  1. >>处理的后文件.txt
复制代码
执行太多次了,这写文件操作特别费时。每行都调一次这个,时间都去哪儿了?=




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