假设我有以下数据流:
BODY1
attrib1: someval11
attrib2: someval12
attrib3: someval13
BODY2
attrib1: someval21
attrib2: someval22
attrib3: someval23
BODY3
attrib1: someval31
attrib2: someval32
attrib3: someval33
我想为每个BODY只提取attrib1和attrib3,即
attrib1: someval11
attrib3: someval13
attrib1: someval21
attrib3: someval23
attrib1: someval31
attrib3: someval33
我试grep 'attrib1\|attrib3'
,根据this site,但没有返回任何内容。grep attrib1
和grep attrib2
确实返回数据,但仅针对指定的单个模式。
6条答案
按热度按时间nwnhqdif1#
grep -e 'attrib1' -e 'attrib3' file
man
页面:-e模式,--regexp=模式
使用PATTERN作为模式。这可用于指定多个搜索模式,或保护以连字符(-)开头的模式。(-e由POSIX指定。
*编辑: 或者 *,您可以将图案保存在文件中,并使用
-f
选项:bd1hkmkf2#
非常简单的命令:
jm81lzqq3#
还有egrep;
igetnqfo4#
这在GNU grep 2.6.3中可以工作。
或
nnvyjq4y5#
这取决于你进入的 shell 。
grep -iw 'patter1\|patter2\|pattern3'
在bash shell上工作,因为它在korn shell上不工作。对于korn shell,我们可能不得不尝试grep -e pattern1 -e patter2
等等。2w3kk1z56#
此外,如果有多个模式,请确保将e选项作为最后一个选项。比如说,
grep -iwe 'the' -e 'that' -e 'then' -e 'those'文本
或
grep -iwe 'the' -iwe 'that' -e 'then' -e 'those'文本或grep -wie 'the' -wie 'that' -wie 'then' -wie 'those'文本
但如果你把e放在第一位
grep -eiw 'the' -e 'that' -e 'then' -e 'those'文本
则返回错误。