Grep for literal strings

I use the grep-type tool to search for string literals. I am looking for the appearance of a line in a log file as part of a line in a separate log file. The search text can contain all kinds of special regular expression characters, for example []().*^$-\ .

Is there a Unix search utility that will not use a regex, but just search for string occurrence literals?

+60
unix grep
Jul 14 '10 at 2:01
source share
5 answers

You can use grep for this with the -F option.

 -F, --fixed-strings PATTERN is a set of newline-separated fixed strings 
+83
Jul 14 '10 at 2:08
source share

This is either fgrep or grep -F , which will not execute regular expressions. fgrep is identical to grep -F , but I prefer not to worry about arguments, being essentially lazy :-)

 grep -> grep fgrep -> grep -F (fixed) egrep -> grep -E (extended) rgrep -> grep -r (recursive, on platforms that support it). 
+7
Jul 14 '10 at 2:11
source share

you can also use awk, since it has the ability to find a fixed string, as well as programming options, for example, only

 awk '{for(i=1;i<=NF;i++) if($i == "mystring") {print "do data manipulation here"} }' file 
+3
Jul 14 '10 at 2:16
source share

Go -F to grep .

+2
Jul 14 '10 at 2:05
source share
 cat list.txt one:hello:world two:2:nothello three:3:kudos grep --color=always -F"hello three" list.txt 

Exit

 one:hello:world three:3:kudos 
0
Mar 09 '16 at 11:09
source share



All Articles