Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file contains some text (not regex) in Unix

Tags:

grep

unix

I want to check if a multiline text matches an input. grep comes close, but I couldn't find a way to make it interpret pattern as plain text, not regex.

How can I do this, using only Unix utilities?

like image 960
user1527166 Avatar asked Jan 31 '13 17:01

user1527166


2 Answers

Use grep -F:

-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.)

EDIT: Initially I didn't understand the question well enough. If the pattern itself contains newlines, use -z option:

   -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.

I've tested it, multiline patterns worked.

like image 151
Anton Kovalenko Avatar answered Oct 02 '22 20:10

Anton Kovalenko


From 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.)
like image 37
Zombo Avatar answered Oct 02 '22 19:10

Zombo