2013-01-31 17 views

回答

1

man grep

-F, --fixed-strings 
     Interpret PATTERN as a list of fixed strings, separated by 
     newlines, any of which is to be matched. (-F is specified by 
     POSIX.) 
+0

除非我失去了一些东西,我不认为我可以匹配多行输入与此。例如,匹配“z \ na \ nb \ nc”文件中的“a \ nb”? – user1527166

1

使用grep -F

-F, --fixed-strings

解释图案作为固定的字符串,并以换行,其中的任何一个是被匹配分隔的列表。 (-F由 POSIX指定)。

编辑:最初我不明白的问题不够好。如果模式本身包含换行符,使用-z选项:

-z, --null-data 
      Treat the input as a set of lines, each terminated by a zero 
      byte (the ASCII NUL character) instead of a newline. Like the 
      -Z or --null option, this option can be used with commands like 
      sort -z to process arbitrary file names. 

我测试过它,多模式的合作。

1

如果你试图匹配不包含空行(例如,它不具有两个连续的换行符),输入字符串,你可以这样做:

awk 'index($0, "needle\nwith no consecutive newlines") { m=1 } 
    END{ exit !m }' RS= input-file && echo matched 

如果你需要找到一个字符串连续换行符,将RS设置为某个不在文件中的字符串。 (请注意,如果将RS设置为多个字符,则awk的结果是未指定的,但大多数awk将允许它为字符串。)如果您愿意将搜索的字符串设置为正则表达式,并且您的awk支持设置RS到多个字符,你可以这样做:

awk 'END{ exit NR == 1 }' RS='sought regex' input-file && echo matched 
相关问题