2

How do I get modern coreutils on mac?


I ran into this problem using csplit:

foo.txt:

foo
1
foo
2
foo
3
$: csplit foo '^foo$' '{*}'
# error

Double checking the manpage, man csplit, csplit on Mac is the FreeBSD version and does not offer the '{*}' option. In fact, I must provide the exact number of splits ahead of time. This will either trigger a czplit re-implementation by me, or maybe I can get GNU coreutils on mac.

Is there a way?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Chris
  • 894
  • 5
  • 18
  • I agree, this isn't optimal, but honestly, if this isn't performance-critical, you can just use your mac's default shell, `zsh` with its rather capable loops and regex operators to just do what you want. – Marcus Müller Dec 20 '22 at 15:50
  • GNU coreutils is easily installable from Homebrew. But because these utilities differ markedly from the BSD ones, the commands it adds to PATH have a `g` prefix (e.g., `gls`). You can add the directory containing the non-prefixed ones to `PATH` if you don't care about potentially breaking scripts. – muru Dec 20 '22 at 16:47

1 Answers1

4

GNU coreutils may be installed on macOS via, e.g., Homebrew.

Assuming that an initial installation of Homebrew has been made (see https://brew.sh), coreutils would be installed with

brew install coreutils

Homebrew will add a g prefix to the utilities that conflict with macOS base system utilities so that you get gls, gcp, gcsplit, etc.

To access the GNU coreutils without the g prefix, you may add the $HOMEBREW_PREFIX/opt/coreutils/libexec/gnubin directory to your PATH.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936