tags:

views:

53

answers:

4

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?

A: 

Pass -F to grep.

Ignacio Vazquez-Abrams
+6  A: 

You can use grep for that, with the -F option.

-F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
Scott Stafford
+2  A: 

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).
paxdiablo
For GNU grep, fgrep is just provided as a symlink to grep which makes it take -F
Daenyth
That trick's been around for a long time :-) In early (and possibly current, in fact my Ubuntu box ends up with both of them pointing to vim) UNIXes where disk space was precious, `ex` and `vi` were the same executable and it just checked `argv[0]` to decide on the behaviour. Damnably clever, those early UNIX bods.
paxdiablo
A: 

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
ghostdog74