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.
Asked
Active
Viewed 4.0k times
86

Gilles 'SO- stop being evil'
- 829,060

polarise
- 1,051
4 Answers
102
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

Stéphane Chazelas
- 544,893
90
Yes:
- Install Homebrew
brew install coreutils
apparently not needed with latest Homebrew, see comment by Ran Ever-Hadani belowln -s /usr/local/bin/gtac /usr/local/bin/tac
or use MacPorts to install coreutils
in a similar way.

grebneke
- 4,671
-
2Addition 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 '19 at 21:34
-
1
1
One temporary solution could be:
alias tac='perl -e "print reverse(<>)"'

JJoao
- 12,170
- 1
- 23
- 45
-
1This just caused me to learn that
<>
doesn't only read from STDIN, but optionally from@ARGV
. https://stackoverflow.com/questions/29020883/less-than-and-greater-than-symbols-together I still don't know ifreverse
will load the whole file into memory before outputting the lines in reverse order. That would be terrible. – Bruno Bronosky Apr 09 '19 at 18:11 -
1@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 '19 at 07:45
0
Install gnu coreutils already compiled with Rudix:
sudo rudix install coreutils
Or download and gui install Rudix coreutils
alias tac='tail -r'
will do the trick for OP! – Ketan Feb 07 '14 at 17:18tac() { tail -r -- "$@"; }
– kojiro Mar 01 '16 at 17:44-rs
which will let you reverse at char level instead of new lines? – C-- Nov 07 '19 at 20:00tail
has no equivalent of GNUtac
's-s
option. – Stéphane Chazelas Nov 07 '19 at 21:45