批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[文本处理] [已解决]批处理怎样把文档内容按顺序插入到另一文档里?

  1. 有A,B两个TXT文本
  2. A文本内容包含固定文字如下
  3. 搞不好也他就好
  4. 维尼修斯被判越位
  5. 教程如图所示:
  6. 把球解围出底线
  7. 教程如图所示:
  8. 你好给你个
  9. 会发货人
  10. 在禁区左侧扣球
  11. 教程如图所示:
  12. 自己本场比赛第
  13. 教程如图所示:
  14. 鲁利有犯规嫌疑
  15. 教程如图所示:
  16. 嗨翻天好
  17. 球被鲁利扑出
  18. 教程如图所示:
  19. 萨瓦尔在禁
  20. 教程如图所示:
  21. B文本内容如下:
  22. http://pic176.nipic.com/pic/20180809/3554136_101713740000_4.jpg
  23. http://pic177.nipic.com/pic/20180816/9019994_094939917000_4.jpg
  24. http://pic106.nipic.com/pic/20160810/4244141_155324854000_4.jpg
  25. http://pic173.nipic.com/pic/20180721/19440703_104055015000_4.jpg
  26. http://pic177.nipic.com/pic/20180816/13015598_000808590035_4.jpg
  27. http://pic177.nipic.com/pic/20180815/18549423_163523854000_4.jpg
  28. http://pic168.nipic.com/pic/20180612/22424394_003152436000_4.jpg
  29. http://pic174.nipic.com/pic/20180726/18106716_151104668000_4.jpg
  30. http://pic173.nipic.com/pic/20180722/3052959_191207224082_4.jpg
  31. http://pic174.nipic.com/pic/20180725/22116547_000817999089_4.jpg
  32. http://pic172.nipic.com/pic/20180712/12173849_170332327033_4.jpg
  33. http://pic172.nipic.com/pic/20180712/4244141_234102102081_4.jpg
  34. http://pic166.nipic.com/pic/20180524/9564852_121609847001_4.jpg
  35. http://pic164.nipic.com/pic/20180507/1093457_202441920000_4.jpg
  36. http://pic171.nipic.com/pic/20180703/21630149_174212103081_4.jpg
  37. http://pic166.nipic.com/pic/20180527/12964311_204124198031_4.jpg
  38. http://pic171.nipic.com/pic/20180704/18549423_180635902000_4.jpg
  39. http://pic169.nipic.com/pic/20180618/12173849_191722312033_4.jpg
  40. http://pic169.nipic.com/pic/20180611/13897028_150145756037_4.jpg
  41. http://pic169.nipic.com/pic/20180616/19679582_212518979039_4.jpg
  42. http://pic169.nipic.com/pic/20180614/13219537_195054392083_4.jpg
  43. http://pic169.nipic.com/pic/20180615/12964311_002030999039_4.jpg
  44. http://pic168.nipic.com/pic/20180610/9564852_132714903000_4.jpg
  45. http://pic167.nipic.com/pic/20180530/14142567_145347112081_4.jpg
  46. http://pic167.nipic.com/pic/20180529/3554136_135538408000_4.jpg
  47. http://pic166.nipic.com/pic/20180523/12964311_172326960039_4.jpg
  48. http://pic163.nipic.com/pic/20180503/5412003_223143298000_4.jpg
  49. http://pic162.nipic.com/pic/20180421/23680086_211827291032_4.jpg
  50. http://pic163.nipic.com/pic/20180504/21059944_145326783087_4.jpg
  51. 要做的就是把文本B的图片链接按顺序2个或者4个依次填到文本A “教程如图所示:”的下面
  52. 例如
  53. 搞不好也他就好
  54. 维尼修斯被判越位
  55. 教程如图所示:
  56. http://pic176.nipic.com/pic/20180809/3554136_101713740000_4.jpg
  57. http://pic177.nipic.com/pic/20180816/9019994_094939917000_4.jpg
  58. 把球解围出底线
  59. 教程如图所示:
  60. http://pic106.nipic.com/pic/20160810/4244141_155324854000_4.jpg
  61. http://pic173.nipic.com/pic/20180721/19440703_104055015000_4.jpg
  62. 你好给你个
  63. 会发货人
  64. 在禁区左侧扣球
  65. 教程如图所示:
  66. http://pic177.nipic.com/pic/20180816/13015598_000808590035_4.jpg
  67. http://pic177.nipic.com/pic/20180815/18549423_163523854000_4.jpg
  68. 自己本场比赛第
  69. 教程如图所示:
  70. http://pic168.nipic.com/pic/20180612/22424394_003152436000_4.jpg
  71. http://pic174.nipic.com/pic/20180726/18106716_151104668000_4.jpg
  72. 鲁利有犯规嫌疑
  73. 教程如图所示:
  74. http://pic168.nipic.com/pic/20180612/22424394_003152436000_4.jpg
  75. http://pic174.nipic.com/pic/20180726/18106716_151104668000_4.jpg
  76. 嗨翻天好
  77. 球被鲁利扑出
  78. 教程如图所示:
  79. http://pic173.nipic.com/pic/20180722/3052959_191207224082_4.jpg
  80. http://pic174.nipic.com/pic/20180725/22116547_000817999089_4.jpg
  81. 萨瓦尔在禁
  82. 教程如图所示:
  83. http://pic172.nipic.com/pic/20180712/12173849_170332327033_4.jpg
  84. http://pic172.nipic.com/pic/20180712/4244141_234102102081_4.jpg
复制代码
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

本帖最后由 weizhisheng 于 2019-1-7 14:03 编辑

《教程如图所示:》这个是固定的特征,在这个特征下面加,求大神帮忙。

TOP

本帖最后由 yhcfsr 于 2019-1-7 23:50 编辑

脚本语言:BAT+Powershell 5.0
测试环境:WIN10
思路:在a.txt非空行与空行之间,依次取b.txt的两行插入
  1. <# :********非修改区开始**********
  2. @echo off
  3. more +7 "%~f0" >"%~n0.ps1"
  4. powershell -Mta -NoLogo -NoProfile -ExecutionPolicy bypass -File "%~n0.ps1"
  5. del /f /q "%~n0.ps1"
  6. ********非修改区结束**********#>
  7. $contentA=[io.file]::ReadAllLines('D:\test\work\a.txt',[text.encoding]::Default);#读A文件
  8. $contentB=[io.file]::ReadAllLines('D:\test\work\b.txt',[text.encoding]::Default);#读B文件
  9. [System.Collections.ArrayList] $result=@();
  10. $sum=0;
  11. foreach($lineA in $contentA)
  12. {
  13.     if($lineA){[void] $result.Add($lineA);$blank=$false;}
  14.     else{
  15.         if($contentB[$sum] -and !$blank){do{[void] $result.Add($contentB[$sum++]);}while($sum%2 -ne 0)}#出现空行时,插入b.txt两行
  16.         [void] $result.Add($lineA);
  17.         $blank=$true;
  18.     }
  19. }
  20. if($lineA -and $contentB[$sum]){do{[void] $result.Add($contentB[$sum++]);}while($sum%2 -ne 0)}#a.txt结尾无空行时插入b.txt两行
  21. [io.file]::WriteAllLines('result.txt',$result,[text.encoding]::Default);#输出结果
  22. pause
复制代码
1

评分人数

    • ivor: 简单问题,复杂化了吧技术 + 1
QQ 33892006

TOP

本帖最后由 WHY 于 2019-1-7 20:56 编辑
  1. @if(0)==(0) echo off
  2. cscript //nologo //e:jscript "%~f0" < b.txt
  3. pause & goto :EOF
  4. @end
  5. var arr = [], i = j = 0;
  6. var fso = new ActiveXObject('Scripting.FileSystemObject');
  7. var str = fso.OpenTextFile('a.txt', 1).ReadAll();
  8. while(!WSH.StdIn.AtEndOfStream){
  9.     var n = Math.floor( i++ / 4 );
  10.     if(!arr[n]) arr[n] = [];
  11.     arr[n].push( WSH.StdIn.ReadLine() );
  12. }
  13. str = str.replace(/^教程如图所示:(?=\r\n|$)/gm, function(s){if(arr[j]) s+='\r\n'+arr[j].join('\r\n'); j++; return s});
  14. WSH.Echo(str)
复制代码

TOP

  1. @echo off
  2. (for /f "delims=" %%i in ('findstr /n .* a.txt') do (
  3.     set "str=%%i"
  4.     setlocal enabledelayedexpansion
  5.     set "str=!str:*:=!"
  6.     echo;!str!
  7.     if "!str!" == "教程如图所示:" (
  8.         for /L %%j in (1 1 4) do (
  9.             set "str=" & set /p "str="
  10.             if not "!str!" == "" echo;!str!
  11.         )
  12.     )
  13.     endlocal
  14. )) < b.txt
  15. pause
复制代码
1

评分人数

TOP

本帖最后由 ivor 于 2019-1-7 20:49 编辑

两种方法:
第二个方法更简单一些,如果每次插入5行jpg就修改
(($line += 2)-1)]}  为  (($line += 5)-1)]}

  1. $line = 0;gc a.txt | %{Write-Output $_; if ($_ -match "如图"){gc b.txt | select -Index $line,($line + 1); $line += 2;}} | Out-File c.txt
复制代码
  1. $line = 0;gc a.txt | %{Write-Output $_; if ($_ -match "如图"){(gc b.txt)[$line..(($line += 2)-1)]}} | Out-File c.txt
复制代码
1

评分人数

TOP

简单方法
和 6 楼类似
  1. $TextB=gc b.txt;$n=0;gc a.txt|%{if($_ -notmatch '^$'){$_;if($_ -match "如图"){$TextB[$n..$(($n+=4)-1)];''}}}|sc c.txt
复制代码
再来个二维数组的
  1. Function Input { read-host "等量插入指定数量的图片:输入数字并回车" };$num=Input;
  2. While(!($num -as [int])){Cls; '输入错误,重新输入数字:';$num=Input};cls;
  3. $arr=$TextA=@();gc a.txt|%{if($_ -notmatch '^$'){$arr+=$_}else{if($arr){$TextA+=,$arr;$arr=@()}}};
  4. $TextB=gc b.txt;$n=0;$(for($i=0;$i -lt $TextA.count;$i++){$TextA[$i];$TextB[$n..$(($n+=$num)-1)];''})|sc c.txt;
复制代码
1

评分人数

TOP

  1. gawk "1;/^教程如图所示:$/{for(i=0;i<4;i++){getline s<\"b.txt\";if(s){print s;s=\"\"}}}" a.txt > c.txt
复制代码
1

评分人数

TOP

回复 5# WHY


    谢谢了,已解决。

TOP

回复 6# ivor


    谢谢了,已解决。

TOP

回复 3# yhcfsr


    谢谢了,已解决。

TOP

回复 7# xczxczxcz


    谢谢了,已解决。

TOP

返回列表