How to use 'grep' with characters like * and _?

I need to find some text in one or more files.

This is what I use:

grep -irI "hello" ~/

However, if I want to find a string containing characters like * or _ then these seem to be ignored.

Let's say I want to search in all files for the string a**__.

How can I do that? Many thanks - I just can't figure this out!

0

3 Answers

There is a -F option:

-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.)

This will disable searching by regular expressions and interpreting characters like * or .. Example:

$ cat test
a**__
$ grep -F 'a**__' test
a**__

You should use an escape character (\) in front of * because, otherwise, your string to search will became a regular expression.

For example, if you want to search for **__, you can use:

 grep -irI "\*\*__" ~/

More about: In a regular expression, which characters need escaping?.

A simple double quote (" ") with an escape character ( '\' )will do the job.

for example if I want to search for ind*x.html from the current folder I will use :

grep -r "ind\*x.html" ./

Note : If you are not using without quotes then you need to use two backslash.

grep -r ind\\*x.html ./

Both will search for ind*x.html term in the files.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like