Monday, November 1, 2021

[SOLVED] How to grep in Linux to find lines with literal asterisks in them?

Issue

I'm trying to use grep files in a Linux directory searching for lines which contain the string **Post. The * character is a wildcard, and I can't figure out how to make it literal for this search. For example \*\*Post doesn't work. What's the proper way of escaping the * character so it can be used literally in this case?


Solution

I tested with a file containing the following text:

**Post
*Post
Post

And I would like to grep only the one with **Post

My command is the following

grep -irn "\*\*Post"

The double quote is important.

The result of the command is

a.txt:1:**Post

While the following command

grep -irn "\*Post"

outputs

a.txt:1:**Post
a.txt:2:*Post


Answered By - Pat. ANDRIA