Board logo

标题: [文本处理] [已解决]批处理如何根据文本内容找出另个文本含有相同内容的行? [打印本页]

作者: his    时间: 2016-3-7 17:10     标题: [已解决]批处理如何根据文本内容找出另个文本含有相同内容的行?

两个文本如下:

a.txt文本:
000001,北京张三饭店,早餐
000002,四川馆,午餐
000003,天津发发发,早餐
000004,武汉小食,早餐
000005,武汉小食,晚餐


b.txt文本:
000001
000002
000004


将a和b文本的前面那列数字对比,相同的在b文本里添加a文本数字后面的内容,像这样
000001,北京张三饭店,早餐
000002,四川馆,午餐
000004,武汉小食,早餐


输出到c.txt文本里
作者: ivor    时间: 2016-3-7 17:27

回复 1# his
  1. @echo off
  2. (for /f %%a in (b.txt) do (
  3. findstr %%a a.txt
  4. ))>c.txt
复制代码

作者: happy886rr    时间: 2016-3-7 19:39

  1. @echo off
  2. (for /f %%a in (b.txt) do (
  3. findstr "^%%a," a.txt
  4. ))>c.txt
复制代码
改进了一下,行首数字匹配。防止b.txt的数字太小会紊乱。
作者: pcl_test    时间: 2016-3-7 20:21

  1. findstr /g:b.txt a.txt>c.txt
复制代码

作者: codegay    时间: 2016-3-8 01:54

python
  1. a=open("a.txt").readlines()
  2. b=[r.strip("\n") for r in open("b.txt").readlines()]
  3. with open("c.txt","a+") as f:
  4.         [f.write(r) for r in a if r.split(',')[0] in b]
复制代码





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