It seems that the \w character class in BSD grep matches differently compared to GNU grep. Specifically, GNU grep's \w matches the underscore (_), whereas BSD grep does not. Is this behavior intentional? admin@freebsd14:~ $ cat /etc/os-release NAME=FreeBSD VERSION="14.3-RELEASE" VERSION_ID="14.3" ID=freebsd ANSI_COLOR="0;31" PRETTY_NAME="FreeBSD 14.3-RELEASE" CPE_NAME="cpe:/o:freebsd:freebsd:14.3" HOME_URL="https://FreeBSD.org/" BUG_REPORT_URL="https://bugs.FreeBSD.org/" admin@freebsd14:~ $ echo $LANG C.UTF-8 admin@freebsd14:~ $ grep --version grep (BSD grep, GNU compatible) 2.6.0-FreeBSD admin@freebsd14:~ $ echo a_ | grep -o '\w*' a admin@freebsd14:~ $ ggrep --version ggrep (GNU grep) 3.11 Copyright (C) 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and others; see <https://git.savannah.gnu.org/cgit/grep.git/tree/AUTHORS>. grep -P uses PCRE2 10.45 2025-02-05 admin@freebsd14:~ $ echo a_ | ggrep -o '\w*' a_ admin@freebsd14:~ $
The cool kids use ripgrep or ugrep. echo a_ | rg -o '\w*' a_ echo a_ | ug -o '\w*' 1: _