在一个文件中打印行,在另一文件中匹配模式 [英] Print lines in one file matching patterns in another file

查看:79
本文介绍了在一个文件中打印行,在另一文件中匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件超过40.000行(文件1),我想提取与文件2中的模式匹配的行(约6000行).我像这样使用grep,但是它非常慢: grep -f file2 file1 > out

I have a file with more than 40.000 lines (file1) and I want to extract the lines matching patterns in file2 (about 6000 lines). I use grep like this, but it is very slow: grep -f file2 file1 > out

是否有使用awksed的更快方法?

Is there a faster way to do this using awk or sed?

以下是我文件中的一些摘录:

Here's some extracts from my files:

File1:
scitn003869.2| scign003869 CGCATGTGTGCATGTATTATCGTATCCCTTG
scitn007747.1| scign007747  CACGCAGACGCAGTGGAGCATTCCAGGTCACAA
scitn003155.1| scign003155  TAAAAATCGTTAGCACTCGCTTGGTACACTAAC
scitn018252.1| scign018252  CGTGTGTGTGCATATGTGTGCATGCGTG
scitn004671.2| scign004671  TCCTCAGGTTTTGAAAGGCAGGGTAAGTGCT

File2:
scign000003
scign000004
scign000005
scign004671
scign000013

`

推荐答案

尝试grep -Fwf file2 file1 > out

-F选项指定纯字符串匹配,因此应该更快,而不必使用正则表达式引擎.

The -F option specifies plain string matching, so should be faster without having to engage the regex engine.

这篇关于在一个文件中打印行,在另一文件中匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆