Grep: "^." incorrectly matches

Can someone explain why this code does not work as expected? I would expect it to only match the first character, and it has alphabetic characters, but the wildcard classes (.) And characters behave strangely:

I use -o just to show exactly how things match, it doesn't change what matches at all.

$ echo foo | grep -o '^.'
f
o
o

More unexpected behavior:

$ echo foobarbazquux | grep -o '^[foarqux]'
f
o
o

$ echo foobarbazquux | grep -o '^.[^u]'
fo
ob
ar
ba
zq

Typically, start-string (^) does not behave as expected in these cases. Is there a way to make him behave more normally?

+3
source share
2 answers

Found:

Bug

List of changes

Apparently fixed in 2.5.2. Found it using the launch pad.

+5

Ubuntu 10.04:

marc@panic:~$ echo foo | grep -o '^.'
f
marc@panic:~$ echo foobarbazquux | grep -o '^[foarqux]'
f
marc@panic:~$ echo foobarbazquux | grep -o '^.[^u]'
fo
marc@panic:~$ grep --version
GNU grep 2.5.4

, grep /, , - . , "GREP_OPTIONS".

0

Source: https://habr.com/ru/post/1761818/


All Articles