简体   繁体   English

使用grep提取文件之间的行

[英]Extracting lines between files using grep

I have two files, file1.txt and file2. 我有两个文件,file1.txt和file2。 txt 文本

file1.txt contains multiple strings such as: file1.txt包含多个字符串,例如:

Little.1
Little.2
Little.3

and the file2.txt contains strings such as: 并且file2.txt包含以下字符串:

XYQ Little.1 AGTAGT
ABC Little.12 ATCGCT
GDT Little.3 CGTAGT

How do I use grep so that when I use file1 to grep file2 I only return: 如何使用grep,以便在使用file1到grep file2时仅返回:

XYG Little.1 AGTAGT
GDT Little.3 CGTAGT

Because they match exactly? 因为它们完全匹配?

grep -w and -f are done for this: 为此,完成了grep -w-f的操作:

$ grep -wf file1 file2
XYQ Little.1 AGTAGT
GDT Little.3 CGTAGT

From man grep : 来自man grep

-f FILE, --file=FILE -f FILE,--file = FILE

Obtain patterns from FILE, one per line. 从FILE获取模式,每行一个。 The empty file contains zero patterns, and therefore matches nothing. 空文件包含零个模式,因此不匹配。 (-f is specified by POSIX.) (-f由POSIX指定。)

-w, --word-regexp -w,--word-regexp

Select only those lines containing matches that form whole words. 仅选择包含构成整个单词的匹配项的行。 The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. 测试是匹配的子字符串必须在该行的开头,或者必须在非单词组成字符之前。 Similarly, it must be either at the end of the line or followed by a non-word constituent character. 同样,它必须在行的末尾,或后跟非单词组成字符。 Word-constituent characters are letters, digits, and the underscore. 单词组成的字符是字母,数字和下划线。


Note that without -w it will match other cases: 请注意,如果没有-w ,它将与其他情况匹配:

$ grep -f file1 file2
XYQ Little.1 AGTAGT
ABC Little.12 ATCGCT
GDT Little.3 CGTAGT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM