Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 466:
"Diplomacy is the art of
>>>>></pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec read_lines_reverse lst =
match read_line () with
| line -> read_lines_reverse (line :: lst)
| exception End_of_file -> lst
 
let () = read_lines_reverse [] |> List.iter print_endline</syntaxhighlight>
{{out}}
<pre>
$ ocaml tac.ml <file.txt
--- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
</pre>
 
=={{header|Pascal}}==
Line 612 ⟶ 629:
C:\My Projects\BGI>
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ sharefile drop
[] swap
[ carriage over find split
dup $ "" != while
behead drop
unrot nested swap join
swap again ]
drop nested swap join
witheach [ echo$ cr ] ] is task ( $ --> )
 
$ "rosetta/input.txt" task</syntaxhighlight>
 
{{out}}
 
<pre> --- Will Rodgers
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
</pre>
 
=={{header|R}}==
<syntaxhighlight lang="rsplus">text <- scan("Rodgers.txt", character(), sep = "\n")
Line 780 ⟶ 821:
</pre>
 
=={{header|UNIX ShellRuby}}==
<syntaxhighlight lang="ruby">puts File.readlines("diplomacy.txt").reverse</syntaxhighlight>
{{works with|Bourne Again SHell}}
{{out}}
<pre> --- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
</pre>
=={{header|sed}}==
<syntaxhighlight lang="sed">1!G
h
$!d</syntaxhighlight>
{{out}}
<pre>
$ sed -f tac.sed file.txt
--- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
 
</pre>
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="bash">tac rodgers.txt</syntaxhighlight>
{{out}}
Output:
<pre>
--- Will Rodgers
Line 792 ⟶ 855:
"Diplomacy is the art of
</pre>
 
Notice that '''tac''' is '''cat''' in reverse order.
 
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for File, FileUtil
 
var fileName1 = "rodgers.txt"
9,476

edits