可能重复:
如何在文件中搜索多行模式?使用pcregrep
我正在运行a grep来查找任何*.sql文件,select其后跟单词customerName后跟单词from.此select语句可以跨越多行,并且可以包含制表符和换行符.
我在下面尝试了一些变化:
$ grep -liIr --include="*.sql" --exclude-dir="\.svn*" --regexp="select[a-zA-Z0-
9+\n\r]*customerName[a-zA-Z0-9+\n\r]*from"
Run Code Online (Sandbox Code Playgroud)
然而,这只是永远运行.请问有人能帮助我正确的语法吗?
我需要找到包含特定字符串模式的所有文件.想到的第一个解决方案是使用带有xargs grep的find管道:
find . -iname '*.py' | xargs grep -e 'YOUR_PATTERN'
Run Code Online (Sandbox Code Playgroud)
但是如果我需要找到跨越多行的模式,我就会被卡住,因为vanilla grep找不到多行模式.
我想检查我的所有字符串是否都存在于文本文件中.它们可以存在于同一条线上或不同的线上.部分匹配应该没问题.像这样:
...
string1
...
string2
...
string3
...
string1 string2
...
string1 string2 string3
...
string3 string1 string2
...
string2 string3
... and so on
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我们可以用正则表达式代替字符串.
例如,以下代码检查文件中是否存在任何字符串:
if grep -EFq "string1|string2|string3" file; then
# there is at least one match
fi
Run Code Online (Sandbox Code Playgroud)
如何检查是否所有这些都存在?由于我们只对所有匹配项的存在感兴趣,因此我们应该在所有字符串匹配后立即停止读取文件.
是否有可能做到这一点,而不必调用grep多次(不会标尺时输入文件较大,或者如果我们有一个大量的字符串相匹配),或使用工具,如awk或python?
此外,是否有一个字符串的解决方案可以很容易地扩展为正则表达式?
我正在尝试使用在Cygwin上对此问题的最佳答案中指定的pcregrep 。我的环境是运行Cygwin V 1.7.20(0.266 / 5/3)的Win7 64bit。
使用cygcheck -p pcregrep我得到:
Found 6 matches for pcregrep
libpcre-devel-8.37-1 - libpcre-devel: Perl Compatible Regular Expressions library development (installed binaries and support files)
libpcre-devel-8.37-2 - libpcre-devel: Perl Compatible Regular Expressions library development (installed binaries and support files)
pcre-debuginfo-8.37-1 - pcre-debuginfo: Debug info for pcre (installed binaries and support files)
pcre-debuginfo-8.37-2 - pcre-debuginfo: Debug info for pcre (installed binaries and support files)
pcre-8.37-1 - pcre: Perl Compatible Regular Expressions utilities (installed …Run Code Online (Sandbox Code Playgroud) 我想使用 shell 命令对匹配两种不同模式的两个连续行进行 grep 或搜索,例如匹配 line1:“abc”,用于 line2:“def”。因此,对于以下文本,应该有一个匹配项:第 4 行和第 5 行。
1234
abc-noise
6789
abc-noise
def-noise
def-noise
1234
Run Code Online (Sandbox Code Playgroud)
当我找到这样的匹配时,我想在匹配之前打印它,包括 N 行。有任何想法吗?谢谢。