| Bytes | Lang | Time | Link |
|---|---|---|---|
| 002 | Thunno 2 N | 230729T174443Z | The Thon |
| 009 | Jelly | 170306T130759Z | Erik the |
| 034 | Haskell | 151002T082743Z | wlad |
| 037 | GNU command line environment | 151002T145009Z | peterh |
| 004 | Pyth | 170913T140428Z | KarlKast |
| 067 | Addict | 170913T122845Z | Erik the |
| 052 | tcl | 170717T231340Z | sergiol |
| 006 | Vim | 170530T174039Z | DJMcMayh |
| 145 | C# | 170306T143715Z | Metoniem |
| 052 | Scala | 151002T085538Z | The Arch |
| 072 | Python | 151002T150040Z | m654 |
| 035 | q | 151003T092851Z | Alexande |
| 042 | SNOBOL | 151002T234545Z | ninjalj |
| 171 | C# | 151001T103847Z | LegionMa |
| 055 | ><> | 151002T145029Z | Aaron |
| 027 | GNU Awk | 151002T101948Z | manatwor |
| 052 | Python 2 | 151001T063610Z | Beta Dec |
| 011 | Perl | 151001T071210Z | Dennis |
| 046 | STATA | 151002T042344Z | bmarks |
| 038 | JavaScript SpiderMonkey Shell | 151002T031230Z | Downgoat |
| 325 | Pip | 151001T200530Z | DLosc |
| 065 | Julia | 151001T173218Z | Alex A. |
| 007 | Retina | 151001T165511Z | Martin E |
| 016 | Ruby | 151001T162505Z | daniero |
| 044 | MATLAB | 151001T162351Z | Tom Carp |
| 043 | Bash | 151001T160550Z | manatwor |
| 056 | Pure Bash no external utilities | 151001T153528Z | Digital |
| 017 | Befunge93 | 151001T152527Z | Kevin W. |
| 025 | Bash + common utilities | 151001T152647Z | Digital |
| 091 | JavaScriptNode.js | 151001T145432Z | Ben Fort |
| 007 | sed | 151001T150655Z | Lynn |
| 086 | Hassium | 151001T143720Z | Jacob Mi |
| 025 | Gema | 151001T135549Z | manatwor |
| 006 | Burlesque | 151001T103538Z | Lynn |
| 002 | FlogScript | 151001T103248Z | Lynn |
| 007 | GolfScript | 151001T100252Z | Cristian |
| 041 | Powershell | 151001T091359Z | sweerpot |
| 004 | Pyth | 151001T073201Z | orlp |
| 005 | Pyth | 151001T062850Z | Dennis |
| 016 | Perl | 151001T071027Z | steve |
| 009 | sed | 151001T071311Z | steve |
| 003 | GS2 | 151001T063627Z | recursiv |
| 007 | CJam | 151001T062018Z | Reto Kor |
Haskell, 34 bytes
main=interact$concat.reverse.lines
[edit]
Saved one byte by replacing unlines with concat.
GNU command line environment, 41 40 37 bytes
cat -n|sort -nr|sed 's/^[^ ]* //'
Pyth, 4 bytes
j_.z
Takes input from STDIN
explanation
j_.z
.z # Input as list of lines
_ # reverse
j # join on newline
Addict, 67 bytes
a g
t _[_]
p
o
a p
i _
g
d
a o
d _
s
d
a s
c _[_]
o
d
g
Copy and paste the code here to try it.
Vim, 6 bytes:
!Grev<cr>
Filters every line through the external command: rev. Must be run on linux.
C#, 145 bytes
Not as short as I hoped.. But I tried!
Golfed
using System.Linq;class c{static void Main(string[]a){foreach(var s in System.IO.File.ReadAllLines(a[0]).Reverse())System.Console.WriteLine(s);}}
Ungolfed
using System.Linq;
class c
{
static void Main(string[]a)
{
foreach(var s in System.IO.File.ReadAllLines(a[0]).Reverse())
System.Console.WriteLine(s);
}
}
Scala, 53 52 bytes
(io.Source.stdin.getLines():\0){(a,b)=>println(a);0}
Bastardizing/side-effecting foldRight (:\) to deal with iterators (as returned by getLines) not having reverse
Python, 80 72 bytes
It isn't the best solution, but I guess it's something.
a=lambda x: f=open(x,'r');l=f.readlines();for o in reversed(l): print(o)
Any tips on golfing it further?
q, 35 bytes
-1 each reverse read0 hsym`$.z.x 0;
SNOBOL, 42 bytes
S S =INPUT CHAR(10) S :S(S)
OUTPUT =S
END
C#, 179 171 bytes
using B=System.Console;class A{static void Main(){var a=new System.Collections.Stack();string b;while((b=B.ReadLine())!=null)a.Push(b);foreach(var c in a)B.WriteLine(c);}}
Reads lines, placing them in a stack, and then writes them backwards. I would use Mathematica for this, but it has no sense of EOF.
><>, 55 bytes
0i:1+?!v:a=?v}1+!
0.20r}~< >~}00
oa]~~.?)2l2dor[;!?l
It first counts the length of each line so it can isolate them in a separate stack when it browses back the lines to display them.
It can probably get shorter, I may work on it later.
GNU Awk, 27 characters
(Inspired by Ed Morton's GNU Awk answer. CW as I not intended to hijack his solution.)
{s=$0RT s}END{printf"%s",s}
Note that by changing RT → RS this becomes portable standard Awk but looses the ability to preserve the absence of the final newline.
Sample run:
bash-4.3$ echo -en 'a\nb' | awk '{s=$0RT s}END{printf"%s",s}' | xxd -g 1
0000000: 62 61 0a ba.
bash-4.3$ echo -en 'a\nb\n' | awk '{s=$0RT s}END{printf"%s",s}' | xxd -g 1
0000000: 62 0a 61 0a b.a.
Python 2, 52 bytes
import sys;print''.join(sys.stdin.readlines()[::-1])
Perl, 11 bytes
$\=$_.$\}{
Behaves exactly like tac. This code requires the -p switch, which I have counted as 1 byte.
Test runs
$ echo -en 'a\nb' | perl -pe'$\=$_.$\}{' | xxd -g 1
0000000: 62 61 0a ba.
$ echo -en 'a\nb\n' | perl -pe'$\=$_.$\}{' | xxd -g 1
0000000: 62 0a 61 0a b.a.
How it works
As explained here, the -p switch basically wraps while (<>) { ... ; print } around the program, so the source code is equivalent to
while(<>)
{
$\ = $_ . $\
}
print
For each line of input, we prepend the current line ($_) to $\ (initially undefined), updating the latter with the result.
After all lines have been processed, print prints the value of the local variable $_ (undefined in this scope), followed by the output record separator ($\).
STATA, 46 bytes
inf str99 v using a.b
g a=-_n
so a
l v,noo noh
Expects input as a file called a.b, with each line no more than 99 characters. Reads in a file and labels the variable v. Makes a new variable called a with the negative index of it. Sort by the negative index (i.e. reverse order) and then list every thing in sorted (i.e. reverse) order.
For a longer length of lines, put quotes around each line and use (42 bytes):
insheet using a.b
g a=-_n
so a
l v,noo noh
JavaScript (SpiderMonkey Shell), 38 bytes
[...read(readline())].reverse().join``
Pretty simple
read() reads a file
readline() reads a string from STDIN
[...str] will split str into an array of chars
reverse() will reverse the array
join`` will collpase the array into a string
Pip, 3 + 2 = 5 bytes
Uses the r and n flags; reads from stdin.
RVg
The r flag reads stdin and stores it as a list of lines in g (which is normally a list of command-line args). We then reverse that list, and it is auto-printed. The n flag causes lists to be output with newline as a separator.
Julia, 65 bytes
open(s->print(join(reverse([l for l=readlines(s)]),"")),ARGS[1])
This takes a file as a command line argument and prints its lines in reverse order. Trailing newlines are moved to the front, unlike tac, which is legit.
Ungolfed:
function p(s::Stream)
# Create a vector of the lines of the input stream
L = [l for l in readlines(s)]
# Reverse the vector and join it back into a string
j = join(reverse(L), "")
# Print the string to STDOUT
print(j)
end
# Open the file specified in the first command line argument
# and apply the function p to its contents
open(p, ARGS[1])
Retina, 7 bytes
!rm`.*$
With a single regex, Retina runs in Match mode. This normally just prints the number of matches, but with ! we configure it to print the actual matches instead (separated by linefeeds).
The actual regex is merely .*$. .* matches any line (potentially empty), because . can match any character except linefeeds. I'll get to the $ in a minute.
How do we make it print the matches in reverse? By making use of .NET's right-to-left matching mode, activated with the r. This means the regex engine starts at the end of the string when looking for matches and works backwards.
Finally, the m makes the $ match the end of a line instead of the end of the string. Why do we even need that? The trouble is that .* generates extraneous matches. Consider the regex substitution
s/a*/$0x/
applied to the input baaababaa. You'd think this would yield baaaxbaxbaax, but it actually gives you baaaxxbaxxbaaxx. Why? Because after matching aaa the engine's cursor is between the a and the b. Now it can't match any more as, but a* is also satisfied with an empty string. This means, after every single match you get another empty match.
We don't want that here, because it would introduce additional empty lines, so we discard those extraneous matches (which are at the beginnings of the lines, due to the right-to-left mode) by requiring that matches include the end of the line.
Ruby, 16 bytes
puts [*$<].reverse
MATLAB, 44
@(x) strjoin(fliplr(strsplit(x,'\n')),'\n');
Splits the string at new lines, flips the resulting array, then rejoins with new line characters.
Bash, 48 43 characters
(Inspired by Digital Trauma's Bash answer. Upvotes for the idea should go to him.)
mapfile -c1 -C's=$2$s;set'
printf %s "$2$s"
Sample run:
bash-4.3$ echo -en 'a\nb' | bash tac.sh | xxd -g 1
0000000: 62 61 0a ba.
bash-4.3$ echo -en 'a\nb\n' | bash tac.sh | xxd -g 1
0000000: 62 0a 61 0a b.a.
Pure Bash (no external utilities), 56
mapfile a
for((i=${#a[@]};i--;));{
printf %s "${a[i]}"
}
This is one of the few answers to do exact tac emulation, as asked about in Dennis' comment:
$ echo -en 'a\nb' | ./tacemu.sh | xxd -g 1
0000000: 62 61 0a ba.
$ echo -en 'a\nb\n' | ./tacemu.sh | xxd -g 1
0000000: 62 0a 61 0a b.a.
$
Befunge-93, 17 bytes
~:1+!#v_
>:#,_@>$
Nothing fancy here; just put everything on the stack, then pop it off.
Bash + common utilities, 25
tr \\n ^G|rev|tr ^G \\n|rev
Here the ^G is a literal BEL character. I'm assuming the input is only printable ascii.
This transforms the entire input to one line by replacing newlines with BELs, then reverses that line, then transforms back to multiline, then reverses each line again, to get the desired output.
JavaScript(Node.js), 91 Bytes
console.log(require('fs').readFileSync(process.argv[2])+"".split(d="\n").reverse().join(d))
sed, 7 bytes
G;h;$!d
This works for me (and it's the shortest solution elsewhere), but I don't really want to find out why. I just messed around with the famous 9-byte trick until I found this. I guess Ging the first line does nothing?
Hassium, 90 Bytes 86 Bytes
use IO;func main(){c=File.readLines(args[0]);for(x=c.length-1;x>=0; println(c[x--]))0;
See expanded here
Gema, 25 characters
*\n=@set{s;$0${s;}}
\Z=$s
Sample run:
bash-4.3$ echo -en 'a\nb' | gema '*\n=@set{s;$0${s;}};\Z=$s'
ba
bash-4.3$ echo -en 'a\nb\n' | gema '*\n=@set{s;$0${s;}};\Z=$s'
b
a
FlogScript, 2 bytes
)"
(Try it on anarchy golf.)
The ) enables --in-out-line-array mode, and the rest of the program is ", reversing the array of lines.
Powershell, 41 bytes
$a=$args|%{gc $_};[array]::Reverse($a);$a
Stores the content of a file line by line in a, reverses a and finally prints it.
Pyth, 4 bytes
j_.z
.z is the input separated by lines as a list, _ reverses it and j joins it by a character, which by default is \n.
Pyth, 5 bytes
jb_.z
This reverses the order of the lines with a simple split-reverse-join approach, but not quite like tac.
Perl, 16 bytes
print reverse<>
sed, 9 bytes
1!G;h;$!d
No upvote wanted, this is a famous sed one-liner.
GS2, 3 bytes
* +
The three bytes are, in order, split lines, reverse, and join lines.
CJam, 7 bytes
qN/W%N*
Reads stdin, prints to stdout.
Explanation:
q Get input.
N/ Split at newlines.
W% Reverse list.
N* Join with newlines.