简体   繁体   English

如何从unix中具有两个已知和未知字符串的文件中进行grep?

[英]how to grep from a file with two known and unknown strings in unix?

I am trying to grep the lines containing two strings. 我试图grep包含两个字符串的行。 The first string is "ulist" and the second string can have anything in prefix, but should end with string "path". 第一个字符串是“ ulist”,第二个字符串可以有任何前缀,但应以字符串“ path”结尾。 Only those lines need to be grepped, which contain both of the strings. 只需要对包含这两个字符串的行进行grepped。

I have tried using the below grep condition- 我尝试使用以下grep条件-

agrep -w "ulist;path" // AND between both patterns as both are required in order to grep a line

This command is almost grepping everything that I want but then it does not get those lines, which contain "ulist" and "sipath"(ending with string path but still not grepped). 该命令几乎将我想要的所有东西都变成grep,但是却没有得到那些包含“ ulist”和“ sipath”的行(以字符串路径结尾,但仍然没有grepped)。 I need to grep all lines that contain "ulist" and "*path" (there can be anything before path). 我需要grep包含“ ulist”和“ * path”的所有行(path之前可以有任何内容)。 But, these both are required (AND between these two patterns). 但是,这两个都是必需的(这两个模式之间必须是AND)。

Can anybody suggest the right regex pattern? 有人可以建议正确的正则表达式模式吗?

if first should come before the second 如果第一个应该在第二个之前

$ grep '\bulist\b.*path\b' file

word boundaries to specify words appear not as substrings. 用于指定单词的单词边界不显示为子字符串。

grep -w ".*ulist.*path$" myfile.txt

通过以$结尾,您可以确保字符串'path'在行尾。

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

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