133

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?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Ben
  • 6,567
  • 10
  • 42
  • 64
  • Wonder why the question wasn't _"Can I make `grep` search for literal strings?"_ instead of _"Is there something like `grep` that can search for literal strings?"_ Flat-head screwdrivers can fit Philips screw-heads, you know `;)` – ADTC Dec 02 '17 at 21:14

6 Answers6

175

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

-F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
  • *"newline-separated fixed strings"* How can I do this on the terminal prompt? I know I can create a pattern file, but without a file, is it possible to do on the prompt? Pressing Enter obviously executes the command. – ADTC Dec 07 '15 at 09:21
  • 18
    *I will answer my own question. :)* You just need to provide the multiple fixed strings using repeats of the `-e` option. Like this: `grep -F -e "fixed1" -e "fixed2" -e "fixed3" -e "fixed4"`. No newlines required ;) – ADTC Dec 07 '15 at 09:30
  • 3
    Not available on Solaris. Instead `fgrep` is used. – majkinetor Jun 17 '16 at 11:29
  • 4
    @ADTC I found out I had to use single quotes for the searched string containing only special characters, else nothing will be found. So this didn't return any result: `grep --include=\*.php -FRn -e "$$"` Using single quoutes gave me the wanted result: `grep --include=\*.php -FRn -e '$$'` – Piemol Nov 15 '17 at 13:16
  • @Piemol it could be because `$` has special meaning in `bash`, which takes precedence over `grep`. Single quote overrides that special meaning. **I'm not 100% sure though.** – ADTC Nov 15 '17 at 16:39
  • 4
    You _are_ correct @ADTC. `$` and a few other characters are special and they will be replaced by bash after you hit enter and before `grep` (or whatever command) is executed. You can tell bash to leave all characters (except a single quote) untouched by enclosing them inside single quotes. If you need to type a single quote do it like this `'I'\''m special'` – ndemou Dec 02 '17 at 10:51
20

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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • For GNU grep, fgrep is just provided as a symlink to grep which makes it take -F – Daenyth Jul 14 '10 at 02:12
  • 4
    `egrep` and `fgrep` are not specified by the POSIX standard; `grep -F` and `grep -E` [are](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html) – Richard Hansen Jan 31 '12 at 21:20
4

Pass -F to grep.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    I read this as `Press F to grep` instead of what was actually written, which made me think it was like those `Press F to show respects` or `Press X to doubt` memes. I should get off the internet... – Wimateeka Dec 15 '20 at 18:48
  • 1
    You're not the only one @Wimateeka :) – Dave Bry Aug 03 '21 at 15:32
3

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
  • 327,991
  • 56
  • 259
  • 343
3

I really like the -P flag available in GNU grep for selective ignoring of special characters.

It makes grep -P "^some_prefix\Q[literal]\E$" possible

from grep manual

-P, --perl-regexp Interpret I as Perl-compatible regular expressions (PCREs). This option is experimental when combined with the -z (--null-data) option, and grep -P may warn of unimplemented features.

anapsix
  • 1,847
  • 20
  • 18
2
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
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140