-1

When i use a dot(.) in globbing pattern, it successfully matches this bunch of addresses:

~$ ip addr show | grep i.et
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host 
inet 192.168.1.101/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp3s0
inet6 fe80::c705:4836:6aa7:d321/64 scope link noprefixroutet

At the same time, the question-marked version matches nothing:

~$ ip addr show | grep i?et

Any ideas?

SecretAgentMan
  • 2,690
  • 6
  • 16
  • 35
  • You're confusing regular expressions and filename wildcards. – Barmar Mar 10 '20 at 20:59
  • 1
    `?` matches any character in wildcards, `.` matches any character in regular expressions. `grep` only does regular expressions. – Barmar Mar 10 '20 at 21:00
  • Escape it: `\?` – jeremysprofile Mar 10 '20 at 21:02
  • and always quote regular expressions given on the command line – Greg A. Woods Mar 10 '20 at 21:52
  • Does this answer your question? [How to match "any character" in regular expression?](https://stackoverflow.com/questions/2912894/how-to-match-any-character-in-regular-expression) – jeremysprofile Mar 10 '20 at 22:22
  • Note, too, that `i?et`, as a filename pattern, could match one or more files in the current directory, resulting in those names being passed as arguments, not the literal string `i?et`. – chepner Mar 10 '20 at 22:42
  • As unmatched patterns are treated literally by default in `bash`, `grep` will *probably* get the regular expression `i?et` as its argument, which matches an optional `i` followed by `et`. – chepner Mar 10 '20 at 22:43

1 Answers1

1

Grep's patterns are regular expressions. The question mark you are trying to use is part of Bash's globbing (pattern matching).

Example using globbing:

ls -1d /proc/191?

returns :

/proc/1910
/proc/1913
/proc/1914
/proc/1915
/proc/1916
/proc/1918
/proc/1919

Now with grep's regular expressions (regex):

ls -1 /proc | grep '191.'

returns:

1910
1913
1914
1915
1916
1918
1919

Hope that helps clear up the confusion.

superboot
  • 111
  • 5