Grep for literal strings
UnixGrepUnix Problem Overview
I'm after a grep-type tool to search for purely literal strings. I'm looking for the occurrence of a line of a log file, as part of a line in a seperate log file. The search text can contain all sorts of regex special characters, e.g., []().*^$-\
.
Is there a Unix search utility which would not use regex, but just search for literal occurrences of a string?
Unix Solutions
Solution 1 - Unix
You can use grep for that, with the -F option.
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
Solution 2 - Unix
That's either fgrep
or grep -F
which will not do regular expressions. fgrep
is identical to grep -F
but I prefer to not have to worry about the arguments, being intrinsically lazy :-)
grep -> grep
fgrep -> grep -F (fixed)
egrep -> grep -E (extended)
rgrep -> grep -r (recursive, on platforms that support it).
Solution 3 - Unix
Pass -F
to grep
.
Solution 4 - Unix
you can also use awk, as it has the ability to find fixed string, as well as programming capabilities, eg only
awk '{for(i=1;i<=NF;i++) if($i == "mystring") {print "do data manipulation here"} }' file
Solution 5 - Unix
cat list.txt
one:hello:world
two:2:nothello
three:3:kudos
grep --color=always -F"hello
three" list.txt
output
one:hello:world
three:3:kudos