Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep with wildcard symbols

Wildcard search with grep

I have a file that contains many IP addresses. I wanted to list all the ip addresses in the file and I used grep with a pattern 192.16* but it doesn't show the whole list of IP addresses. I am able to list the whole IP addresses only while using period followed with an asterisk symbol. So my doubt is Why 2nd option not working but 3rd option works fine.

root@test:~/test# cat z
192.168.1.0
192.168.2.0
192.168.110.7
192.168.115.5

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


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

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


4. root@test:~/test# grep -o 192.1.* z
192.168.1.0
192.168.2.0
192.168.110.7
192.168.115.5
like image 824
theG Avatar asked Oct 20 '25 13:10

theG


2 Answers

  • A dot (.) matches any character, you have to escape it: \..

  • -o shows only the matching part, if you ommit .* (= any characters) from the end, it will ommit the rest of the line (as it's not part of the matched string).

  • .* can match a lot more than you need (it will match the rest of the line), prefer to say explicitly what you allow: [0-9.]*.

  • Make sure you put the search expression in single quotes '192.168\.[0-9.]*', otherwise the shell will interpret the special characters, and substitute the expression with the matched filenames (luckily you didn't have any matching filenames).

  • You might only want to searh for words (-w). If you want to make sure you only match IP addresses and not something that resembles to it (no consecutive dots, exactly 4 digits, <=255...) then you'll need a lot more complex expression.

like image 101
Karoly Horvath Avatar answered Oct 23 '25 03:10

Karoly Horvath


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.

like image 39
Michael Jaros Avatar answered Oct 23 '25 04:10

Michael Jaros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!