找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 35521|回复: 7

[文本处理] 求助批处理文本筛选找出两个文件前两列相同的行

[复制链接]
发表于 2019-5-23 16:57:50 | 显示全部楼层 |阅读模式
假如A文本有
aaaa----aaaa
bbbb----bbbb
cccc----cccc
dddd----dddd
eeee----eeee
B文本有
aaaa----aaaa----aaaa
bbbb----bbbb----bbbb
cccc----cccc----cccc

导出C文本内有这两个文本前两段相同的到C文本
aaaa----aaaa----aaaa
bbbb----bbbb----bbbb
cccc----cccc----cccc
发表于 2019-5-23 17:17:33 | 显示全部楼层
  1. findstr /g:"A.txt" "B.txt" > "C.txt"
复制代码
发表于 2019-5-23 17:19:39 | 显示全部楼层
本帖最后由 zaqmlp 于 2019-5-23 18:16 编辑
  1. @echo off
  2. set info=互助互利,支付宝扫码头像,感谢赞助
  3. rem 有问题,可加QQ956535081及时沟通
  4. title %info%
  5. cd /d "%~dp0"
  6. set "file1=A.txt"
  7. set "file2=B.txt"
  8. set "file3=C.txt"
  9. powershell -NoProfile -ExecutionPolicy bypass ^
  10.     $dic=New-Object 'System.Collections.Generic.Dictionary[string,string]';^
  11.     [System.Collections.ArrayList]$s=@();^
  12.     $atxt=[IO.File]::ReadAllLines('%file1%',[Text.Encoding]::Default);^
  13.     for($i=0;$i -lt $atxt.Count;$i++){^
  14.         if($atxt[$i].trim() -match '^^.+?----.+?(?=----^|$)'){^
  15.             if(-not $dic.ContainsKey($matches[0])){$dic.add($matches[0],'')};^
  16.         };^
  17.     };^
  18.     $btxt=[IO.File]::ReadAllLines('%file2%',[Text.Encoding]::Default);^
  19.     for($i=0;$i -lt $btxt.Count;$i++){^
  20.         if($btxt[$i].trim() -match '^^.+?----.+?(?=----^|$)'){^
  21.             if($dic.ContainsKey($matches[0])){[void]$s.add($btxt[$i])};^
  22.         };^
  23.     };^
  24.     [IO.File]::WriteAllLines('%file3%', $s, [Text.Encoding]::Default);^
  25.     write-host '%info%' -ForegroundColor green;
  26. pause
复制代码
发表于 2019-5-23 17:28:41 | 显示全部楼层
  1. powershell "gc a.txt | %{findstr "$_" b.txt | Out-File c.txt -Append}"
复制代码
发表于 2019-5-24 16:49:52 | 显示全部楼层
回复 2# Batcher

版主大佬为什么用这个代码后第一行看不到,要怎么弄才能看到第一行
发表于 2019-5-24 17:59:56 | 显示全部楼层
回复 5# miqilaosu


    把你测试用的txt文件和bat文件打包压缩上传,我试试。
发表于 2019-5-25 09:11:12 | 显示全部楼层
回复 6# Batcher

我试了好多次都是少第一行,不知道为什么
发表于 2019-5-25 21:55:46 | 显示全部楼层
回复 7# miqilaosu


    你这个1.txt是UTF-8编码,先转换成ANSI编码再试试。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 05:14 , Processed in 0.018385 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表