Reputation: 6571
When I run the following:
printf "1234\n1234" | grep -o '^.'
I get the following output on OSX 10.10 with zsh 5.0.5:
1
2
3
4
1
2
3
4
Why? I'd expect this output instead:
1
1
Can anyone reproduce this issue? I get the expected output on Ubuntu 14.04.
Upvotes: 3
Views: 2163
Reputation: 77137
The problem seems to be that Apple has elected to have a more-than-a-decade-old version of grep
on its latest operating system. It has bugs including the one you point out. The only thing I can seriously suggest is that you use a package manager like homebrew to upgrade the grep you use for day-to-day work.
$ grep --version
grep (BSD grep) 2.5.1-FreeBSD
$ grep --color=never -o ^. <<< ab
a
b
$ brew install grep
==> Installing grep from homebrew/homebrew-dupes
<SNIP>
$ ggrep --version
ggrep (GNU grep) 2.21
$ ggrep --color=never -o ^. <<< ab
a
Upvotes: 7
Reputation: 41456
I can not tell you what is wrong, but try a workaround:
printf "1234\n1234" | awk -vFS= '{print $1}'
1
1
By setting FS
to nothing, every characters become separate fields, then print first field $1
If -FS=
does not work try -vFS=""
Upvotes: 0