Using the star sign in grep

RegexBashGrep

Regex Problem Overview


I am trying to search for the substring "abc" in a specific file in linux/bash

So I do:

grep '*abc*' myFile

It returns nothing.

But if I do:

grep 'abc' myFile

It returns matches correctly.

Now, this is not a problem for me. But what if I want to grep for a more complex string, say

*abc * def *

How would I accomplish it using grep?

Regex Solutions


Solution 1 - Regex

The asterisk is just a repetition operator, but you need to tell it what you repeat. /*abc*/ matches a string containing ab and zero or more c's (because the second * is on the c; the first is meaningless because there's nothing for it to repeat). If you want to match anything, you need to say .* -- the dot means any character (within certain guidelines). If you want to just match abc, you could just say grep 'abc' myFile. For your more complex match, you need to use .* -- grep 'abc.*def' myFile will match a string that contains abc followed by def with something optionally in between.

Update based on a comment:

* in a regular expression is not exactly the same as * in the console. In the console, * is part of a glob construct, and just acts as a wildcard (for instance ls *.log will list all files that end in .log). However, in regular expressions, * is a modifier, meaning that it only applies to the character or group preceding it. If you want * in regular expressions to act as a wildcard, you need to use .* as previously mentioned -- the dot is a wildcard character, and the star, when modifying the dot, means find one or more dot; ie. find one or more of any character.

Solution 2 - Regex

The dot character means match any character, so .* means zero or more occurrences of any character. You probably mean to use .* rather than just *.

Solution 3 - Regex

The "star sign" is only meaningful if there is something in front of it. If there isn't the tool (grep in this case) may just treat it as an error. For example:

'*xyz'    is meaningless
'a*xyz'   means zero or more occurrences of 'a' followed by xyz

Solution 4 - Regex

Use grep -P - which enables support for Perl style regular expressions.

grep -P "abc.*def" myfile

Solution 5 - Regex

The expression you tried, like those that work on the shell command line in Linux for instance, is called a "glob". Glob expressions are not full regular expressions, which is what grep uses to specify strings to look for. Here is (old, small) post about the differences. The glob expressions (as in "ls *") are interpreted by the shell itself.

It's possible to translate from globs to REs, but you typically need to do so in your head.

Solution 6 - Regex

You're not using regular expressions, so your grep variant of choice should be fgrep, which will behave as you expect it to.

Solution 7 - Regex

This worked for me:

grep ".*${expr}" - with double-quotes, preceded by the dot. Where ${expr} is whatever string you need in the end of the line.

So in your case:

grep ".*abc.*" myFile

Standard unix grep.

Solution 8 - Regex

Try grep -E for extended regular expression support

Also take a look at:

The grep man page

Solution 9 - Regex

'*' works as a modifier for the previous item. So 'abc*def' searches for 'ab' followed by 0 or more 'c's follwed by 'def'.

What you probably want is 'abc.*def' which searches for 'abc' followed by any number of characters, follwed by 'def'.

Solution 10 - Regex

This may be the answer you're looking for:

grep abc MyFile | grep def

Only thing is... it will output lines were "def" is before OR after "abc"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaobiView Question on Stackoverflow
Solution 1 - RegexDaniel VandersluisView Answer on Stackoverflow
Solution 2 - RegexsmcameronView Answer on Stackoverflow
Solution 3 - RegexanonView Answer on Stackoverflow
Solution 4 - RegexArtem RussakovskiiView Answer on Stackoverflow
Solution 5 - RegexunwindView Answer on Stackoverflow
Solution 6 - RegexAndrew BealsView Answer on Stackoverflow
Solution 7 - Regexaccess_grantedView Answer on Stackoverflow
Solution 8 - RegexBrianView Answer on Stackoverflow
Solution 9 - RegexConspicuous CompilerView Answer on Stackoverflow
Solution 10 - RegexCharles DukeView Answer on Stackoverflow