Grep for Text with Wild Card in Between

Using the star sign in grep

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.

grep for text with wild card in between

You need to grep for something like "==> .*\.sh <=="

The .* part matches any character for any length, the \. part matches a dot.

grep with wildcard symbols

Why your commands are (not) working:

1. root@test:~/test# grep -o 192.1 z

Only 192<any char>1 will be matched, and only the matching part will be printed because of the -o switch.

2. root@test:~/test# grep -o 192.1* z

Only 192<any char>, 192<any char>1, 192<any char>11, 192<any char>111 etc. will be matched, and only the matching part will be printed because of the -o switch. Your input does not contain data where this makes any difference.

3. root@test:~/test# grep -o 192.1. z

Only 192<any char>1<any char> will be matched, and only the matching part will be printed because of the -o switch. This gives you one more character (. stands for "any" single character).

4. root@test:~/test# grep -o 192.1.* z

Any line starting with 192<any char>1 will be matched, and only the matching part will be printed because of the -o switch. .* matches anything up to the end of the line, including the empty string.

Regular expression for IP addresses

You can find lots of IP address regular expressions on the web, see for example this StackOverflow question. Note however that some of the expressions are used to match only the IP address and therefore contain beginning- (^) and end-of-line ($) characters. You will have to remove those if your input contains more than just the addresses.

grep multipe wildcards in string

Your attempt is very close. * in shell glob terms is roughly equivalent to .* in regex terms. . means "any character" and * is means "repeated any number of times (including zero).

Your regex just needs . before each *. The trailing * is not necessary:

package.*el6.*x86_64

Here is a sample run with your input:

grep 'package.*el6.*x86_64' <<< "Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_i686.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_i686.rpm"

Prints:

Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm

Using grep to find whole word with wildcard and exclusions

You may use

grep -ioh "\S*ice\S*"

Or

grep -ioh "[^[:space:]]*ice[^[:space:]]*"

The [^[:space:]] (or \S) match any char but a whitespace char.

See the grep online demo:

grep -ioh "\S*ice\S*" <<< "The cat is very-nice"
## => very-nice

expect certain number of characters in a wildcard with grep

First make sure to use anchors in foo.txt to make sure to match complete line:

cat foo.txt

^.{64}  /Users/1337/X$
^.{64} /Users/1337/R$

Then use this grep command to get your results:

grep -Ef foo.txt bar.txt

1121cfccd5913f0a63fec40a6ffd44ea64f9dc135c66634ba001d10bcf4302a2  /Users/1337/R
53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3 /Users/1337/X

-E option is needed to support extended regex in grep command.

Shell UNIX : grep wild card

The first argument to grep is not a wildcard, it's a regular expression. In a regular expression, * means to match any number of the character or expression that precedes it. So

grep "tgt/etc/*"

means to match tgt/etc followed by zero or more / characters. In a wildcard, * means to match any number of any characters, the equivalent regular expression is .*. For your purposes, the commands you want are:

find . -type f -name \* | grep "tgt/etc/"
find . -type f -name \* | grep "tgt/et.*/s"

Also, if you don't quote the argument, and it contains any * characters, the shell will expand the argument as a filename wildcard before passing them as arguments to grep. So when you write:

find . -type f -name \* | grep tgt/etc/*

the shell will expand this to

find . -type f -name \* | grep tgt/etc/file1 tgt/etc/file2 tgt/etc/file3

This will treat the tgt/etc/file1 as the regular expression to search for, and look for it inside the remaining files -- it will not process the input from the pipeline because it was given filename arguments.

Using grep with delimiter and wildcard to search information from file

Try this pattern:

grep ':.*fg.*:' ./*


Related Topics



Leave a reply



Submit