Search code examples
grepbsd

Why does grep return lines in the middle of the string when I expect it to be anchored at the beginning?


I'm trying to extract the first word of the line when the line starts with whitespace, so I write the following command. But grep also returns the second word when it shouldn't. The ^ is supposed to match the beginning of the line:

echo -e "    cat   foo\n    dog   bar\n" | grep -Eo '^ +[^ ]+'

Returns:

    cat
   foo
    dog
   bar

I expect it to return:

    cat
    dog

I'm running on MacOS 10.15.7.


Solution

  • As stated here in this report, this is actually a bug in BSD grep.

    As a work around, you can use these awk and sed command to get equivalent output

    cat file
        cat   foo
        dog   bar
    
    sed -E 's/(^[[:blank:]]+[^[:blank:]]+).*/\1/' file
        cat
        dog
    
    awk 'match($0, /^[[:blank:]]+[^[:blank:]]+/){print substr($0, 1, RLENGTH)}' file
        cat
        dog