31

Tried

$ echo "$STRING" | egrep "(\*)"

and also

$ echo "$STRING" | egrep '(\*)'

and countless other variations. I just want to match a line that contains a literal asterisk anywhere in the line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Derrick
  • 2,356
  • 5
  • 32
  • 43

6 Answers6

25

Try a character class instead

echo "$STRING" | egrep '[*]' 
pavium
  • 14,808
  • 4
  • 33
  • 50
  • On Windows with GnuWin32, you can use `grep [*] file.txt`. – Brian Burns Jul 17 '13 at 23:12
  • ... except if you have a file named literally `*` in the directory where you run that. The lesson (not particularly from this example, but generally) is to always quote your regular expressions, no matter how trivial. – tripleee Nov 10 '16 at 06:57
7
echo "$STRING" | fgrep '*'

fgrep is used to match the special characters.

Vivek
  • 11,938
  • 19
  • 92
  • 127
Rohit_Sharma
  • 71
  • 1
  • 3
7

Simply escape the asterisk with a backslash:

grep "\*"
Paul Feakins
  • 719
  • 5
  • 7
6

Use:

grep "*" file.txt

or

cat file.txt | grep "*"
DaveParillo
  • 2,233
  • 22
  • 20
  • 1
    since you can do the first, the second one with cat is not necessary. – ghostdog74 Oct 17 '09 at 04:33
  • I hear ya. Many people seem to like using cat to stream a file into all sorts of programs even though it's typically redundant. I just thought I'd include it to make the example look more familiar. – DaveParillo Oct 17 '09 at 15:07
2

Here's one way to match a literal asterisk:

$ echo "*" | grep "[*]"
*
$ echo "*" | egrep "[*]"
*
$ echo "asfd" | egrep "[*]"
$ echo "asfd" | grep "[*]"
$ 

Wrapping an expression in brackets usually allows you to capture a single special character easily; this will also work for a right bracket or a hyphen, for instance.

Be careful when this isn't in a bracket grouping:

$ echo "hi" | egrep "*"
hi
$ echo "hi" | grep "*"
$
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 1
    Your example shows why I find this behavior of egrep irritating. If you want to do a literal string search with grep, just put it in quotes. Why would I **ever** want to grep for everything? unix calls that program 'cat' ;-) I hate the egrep forces you to bracket regex characters even if they are in double quotes. End of rant. Thanks! – DaveParillo Oct 17 '09 at 15:13
  • 2
    @DaveParillo: neither grep nor egrep know whether the pattern you passed them was quoted or not, as the quotes are interpreted by the shell before [e]grep is launched. The difference is due to how grep and egrep respond when the pattern starts with "*", which is technically malformed ("*" is a suffix). Try `grep ".*"` and you'll see that it matches everything. If you want a literal (non-regex) search, use `fgrep` instead. – Gordon Davisson Oct 17 '09 at 22:22
0

If there is a need to detect an asterisk in awk, you can either use

awk '/\*/' file

Here, * is used in a regex, and thus, must be escaped since an unescaped * is a quantifier that means "zero or more occurrences". Once it is escaped, it no longer has any special meaning.

Alternatively, if you do not need to check for anything else, it makes sense to peform a fixed string check:

awk 'index($0, "*")' file

If * is found anywhere inside a "record" (i.e. a line) the current line will get printed.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563