简体   繁体   English

grep两个字符串/单词的列表,仅返回第一个结果

[英]grep a list of two strings/words and return only the first result

I have a file (biglist.txt) that I need to search for lines that contain two strings and I need the output to only return the first instance of that search. 我有一个文件(biglist.txt),我需要搜索包含两个字符串的行,并且需要输出仅返回该搜索的第一个实例。 String one (of two) is variable, meaning I have a file that is another long line-separated (hard returned) list of different strings called (queries.txt). 字符串一(两个)是可变的,这意味着我有一个文件,该文件是另一个长行分隔(硬返回)的不同字符串列表,称为(queries.txt)。 The second string will be a constant word (let's call the word "description"). 第二个字符串将是一个常量字(我们将其称为“描述”字)。 I need to search for lines with variable string + constant string, and only return the first instance of such a search. 我需要搜索具有可变字符串+常量字符串的行,并且仅返回此类搜索的第一个实例。

I know something like this will work for a list of single strings. 我知道类似这样的方法适用于单个字符串列表。

cat queries.txt | xargs -I{} grep -m 1 {} biglist.txt > output

but I need to add the argument that each of the strings in the queries.txt must also be on a line with the constant word "description". 但我需要添加一个论点,即querys.txt中的每个字符串也必须与常量字“ description”一起位于一行。 The file "biglist.txt" has multiple lines with each of the strings and the word "description" and I only need to output one of those. 文件“ biglist.txt”在每个字符串和单词“ description”中都有多行,我只需要输出其中之一即可。 Not all of the lines have both the string and the word "description" and often the first line does not have the word "description". 并非所有的行都具有字符串和单词“ description”,并且通常第一行没有单词“ description”。 This newb appreciates any help. 此新手感谢您的帮助。

Without a better description of the input and expected output this might do the job: 如果没有对输入和预期输出的更好描述,这可能会完成工作:

grep description biglist.txt | grep -f queries.txt | head -n1

Output the first line in biglist.txt that contains description and one of the queries in queries.txt. 在biglist.txt中输出第一行,该行包含描述和querys.txt中的一个查询。

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

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