Grep for literal strings

UnixGrep

Unix 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - UnixScott StaffordView Answer on Stackoverflow
Solution 2 - UnixpaxdiabloView Answer on Stackoverflow
Solution 3 - UnixIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 4 - Unixghostdog74View Answer on Stackoverflow
Solution 5 - UnixparandhamanView Answer on Stackoverflow