Board logo

标题: [文本处理] 【已解决】批处理如何从文本中筛出想要的信息并置换内容? [打印本页]

作者: sweet惜缘    时间: 2015-12-30 13:49     标题: 【已解决】批处理如何从文本中筛出想要的信息并置换内容?

DDI.txt中内容如下,是我想要筛选的源文件。
drug1 id        drug2 id
DB00005        DB01281
DB00005        DB00026
DB00005        DB08879
DB00005        DB06168
DB00005        DB08904
DB00005        DB00531
DB00005        DB06643
DB00005        DB00065
DB00005        DB01097
index.txt中内容如下:
drug  id
DB00175
DB00177
DB00178
DB00190
DB00191
DB00193
DB00199
DB00201
DB00210
DB00222
DB00227
link.txt中则是drug  id与stitch_id中 一 一对应关系:
CID000054687        DB00175
CID000060846        DB00177
CID005362129        DB00178
CID000034359        DB00190
CID000004771        DB00191
CID000033741        DB00193
CID000012560        DB00199
CID000002519        DB00201
CID000060164        DB00210
CID000003476        DB00222
希望依据index中所存在的196个drug id去筛选DDI.txt中的数据,当且仅当DDI.txt每一行中的drug1 id        drug2 id都在index.txt中出现,该行才可被提取出来输入到out.txt中去,在输入到最终的out.txt之前希望根据link.txt中drug  id与stitch_id中 一 一对应关系将筛选出来的DDI从drug id格式转化成stitch_id格式,结果大致应如下
stitch1_id                 stitch2_id
CID000002118        CID000000896
CID000002249        CID000002162
CID000002315        CID000002249
CID000002481        CID000001983
CID000002540        CID000002162
CID000002576        CID000002244
CID000002712        CID000002160
CID000002732        CID000002249
望大神不吝赐教~~~
作者: flyinnet9    时间: 2015-12-30 14:07

本帖最后由 flyinnet9 于 2015-12-30 14:34 编辑
  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. for /f %%a in (index.txt) do set %%a=1
  4. (for /f "tokens=1-2" %%a in (ddi.txt) do if defined %%a if defined %%b echo.%%a %%b)>tmp.txt
  5. for /f "tokens=1-2" %%a in (link.txt) do set %%b=%%a
  6. (for /f "tokens=1-2" %%a in (tmp.txt) do echo.!%%a! !%%b!)>out.txt
  7. del tmp.txt
复制代码

作者: sweet惜缘    时间: 2015-12-31 14:08

回复 2# flyinnet9

代码有效!灰常感谢!!!




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