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.
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