59

I like to use tac to reverse the output of cat. However, it's not available in the Mavericks terminal. I tried to find it on MacPorts and again it's not available. Can anyone please show me how to get tac? It's very helpful for reading log files.

65

Yes:

  • Install Homebrew
  • brew install coreutils
  • ln -s /usr/local/bin/gtac /usr/local/bin/tac apparently not needed with latest Homebrew, see comment by Ran Ever-Hadani below

or use MacPorts to install coreutils in a similar way.

  • I don't get how ln -s /usr/local/bin/gtac /usr/local/bin/tac made it work... Isn't that command only supposed to list some file memory blocks? – Timothy Swan Sep 27 '17 at 17:51
  • 1
    The symlink is just for convenience. Homebrew installs coreutil binaries in /usr/local/bin by prefixing their original name with a 'g'. ls becomes gls, head becomes ghead and so on. Consequently, tac is installed as gtac, and the symlink suggested in this answer is just to make tac available under that name. Omitting the symlink, you can still run the program as gtac. – grebneke Sep 27 '17 at 21:20
  • Oh. You're talking about ln -s not ls -s This makes more sense now. I must have entered it in correctly the first time. – Timothy Swan Sep 27 '17 at 21:29
  • 2
    Addition to grebneke's answer: no need the soft link anymore. The g suffix is now only added if osx already has a command with that name, so tac is installed as tac, not gtac. – Ran Ever-Hadani Feb 17 at 21:34
68

On OS/X like on many systems (BSDs, Solaris, AIX, IRIX...), the functionality of GNU tac is available in tail with the -r option. So no need to install GNU tac:

tail -r the-file
  • 10
    So, alias tac='tail -r' will do the trick for OP! – mkc Feb 7 '14 at 17:18
  • 7
    Well, actually using a shell alias may fail because aliases don't transfer into shell scripts. Use a function like tac() { tail -r -- "$@"; } – kojiro Mar 1 '16 at 17:44
0

Install gnu coreutils already compiled with Rudix:

sudo rudix install coreutils

Or download and gui install Rudix coreutils

0

One temporary solution could be:

alias tac='perl -e "print reverse(<>)"'
  • 1
    This just caused me to learn that <> doesn't only read from STDIN, but optionally from @ARGV. stackoverflow.com/questions/29020883/… I still don't know if reverse will load the whole file into memory before outputting the lines in reverse order. That would be terrible. – Bruno Bronosky Apr 9 at 18:11
  • @BrunoBronosky, > ...STDIN, but optionally from @ ARGV : yes that is what <> is for. > ... will load the whole file into memory... : YES, IT WILL! (avoid doing this with gigabyte files) – JJoao Apr 10 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.