【发布时间】:2018-08-20 18:58:44
【问题描述】:
我有一个多列文件如下:
file1.txt
1 12 220 AJ-lett-K-MD
2 33 312 BCJ-23-660-numm
4 22 55 lett-C-100b
5 52 59 lett-C-100bc
6 82 995 numm-X-aab
还有,
file2.txt
1 338 339 89839,lett;847447,AJ-lett-K-MD
2 223 443 numm;33920;numm3,AJ-lett-K-MD,50
3 443 223 AFFVlett-C-100b,lett-C-100b
4 542 442 187;lett-C-100bc
7 765 765 XXXX-CCC
我正在尝试在file2.txt 的4th 列中搜索file1.txt 的4th 列,如果存在,则打印file1.txt 的1st,2nd,3rd 列和4th 列file2.txt,在file2.txt的内容之上。
例如,file1.txt 中1st line 的4th 列是AJ-lett-K-MD。它存在于file2.txt 中1st and 2nd lines 的4th 列中。
所以,我需要打印1st line 的file1.txt 的1st,2nd and 3rd colums 和1st and 2nd lines 的file2.txt 的4th columns :
所以预期的输出应该是:
expected.txt
1 338 339 89839,lett;847447,AJ-lett-K-MD --> original
1 12 220 89839,lett;847447,AJ-lett-K-MD --> combination of file1 and file2
2 223 443 numm;33920;numm3,AJ-lett-K-MD,50 --> original
1 12 220 numm;33920;numm3,AJ-lett-K-MD,50 --> combination of file1 and file2
3 443 223 AFFVlett-C-100b,lett-C-100b --> original
4 22 55 AFFVlett-C-100b,lett-C-100b --> combination
4 542 442 187;lett-C-100bc --> original
4 22 55 187;lett-C-100bc --> combination
5 52 59 187;lett-C-100bc --> combination
7 765 765 XXXX-CCC
我的尝试是将file1.txt的单词作为变量,然后在file2.txt中搜索:
grep -v ^# file1.txt | while read a b c d; do echo a=$a b=$b c=$c d=$d;
grep "$d" file2.txt
它给出:
1 338 339 89839,lett;847447,AJ-lett-K-MD
2 223 443 numm;33920;numm3,AJ-lett-K-MD,50
3 443 223 AFFVlett-C-100b,lett-C-100b
4 542 442 187;lett-C-100bc
但是,我不知道如何进行。使用awk 或python 会更好吗?任何帮助表示赞赏!
PS : file1.txt 中的 4th 列不是唯一的,我需要所有匹配项(不仅仅是第一个)。
重要编辑:我以不同且更好的方式解释了我的问题:Searching partial match of string in a column in a column of another file 他们搜索相似但不同的输出。
【问题讨论】:
-
注意,
lett-C-100b这个项目同时出现在3 443 223 AFFVlett-C-100b,lett-C-100b和4 542 442 187;lett-C-100bc中。匹配规则应细化 -
是的,你是对的。在这种情况下,它也会映射到这个。我正在编辑
标签: linux string search awk grep