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

m
(Initial Haskell version.)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 6 users not shown)
Line 25:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">:start:
V fileData = File(:argv[1]).read().split("\n")
 
L(line) reversed(fileData)
print(line)</langsyntaxhighlight>
 
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/rodgers.txt rodgers.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
DEFINE BUFFERSIZE="1000"
BYTE ARRAY buffer(BUFFERSIZE)
Line 87:
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Reverse_the_order_of_lines_in_a_text_file_while_preserving_the_contents_of_each_line.png Screenshot from Atari 8-bit computer]
Line 99:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Containers.Indefinite_Vectors;
with Ada.Command_Line;
Line 136:
end loop;
 
end Reverse_Lines_In_File;</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % reverse the order of the lines read from standard input %
% record to hold a line and link to the next %
record LinkedLine ( string(256) text; reference(LinkedLine) next );
Line 165:
lines := next(lines)
end while_lines_ne_null
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 175:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f REVERSE_THE_ORDER_OF_LINES_IN_A_TEXT_FILE_WHILE_PRESERVING_THE_CONTENTS_OF_EACH_LINE.AWK filename
{ arr[NR] = $0 }
Line 184:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 197:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">source = freefile
open (source, "text.txt")
textEnt$ = ""
Line 213:
next n
close source
end</langsyntaxhighlight>
{{out}}
<pre>
Line 220:
 
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Reverse the order of lines in a text file while preserving the contents of each line. Nigel Galloway: August 9th., 2022
seq{use n=System.IO.File.OpenText("wr.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.rev|>Seq.iter(printfn "%s")
</syntaxhighlight>
{{out}}
<pre>
-- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io io.encodings.utf8 io.files sequences ;
 
"rodgers.txt" utf8 file-lines <reversed> [ print ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 238 ⟶ 251:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">open "text.txt" for input as #1
dim as string textEnt, textSal()
dim as integer n, linea = 0
Line 253 ⟶ 266:
next n
close #1
sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 264 ⟶ 277:
 
=={{header|Free Pascal}}==
<langsyntaxhighlight lang="pascal">program TAC;
{$IFDEF FPC}
{$MODE DELPHI}
Line 287 ⟶ 300:
end;
writeln(Sl.text);
end.</langsyntaxhighlight>{{out}}
<pre> --- Will Rodgers
 
Line 296 ⟶ 309:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 338 ⟶ 351:
}
fmt.Println(string(b))
}</langsyntaxhighlight>
 
{{out}}
Line 350 ⟶ 363:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import qualified Data.Text as T
import qualified Data.Text.IO as TIO
 
main :: IO ()
main = TIO.interact $ T.unlines . reverse . T.lines</langsyntaxhighlight>
{{out}}
<pre>
Line 364 ⟶ 377:
"Diplomacy is the art of
</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang="j"> ;|.<;.2 text
--- Will Rodgers
 
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of</syntaxhighlight>
 
where
<syntaxhighlight lang="j">text=: {{)n
"Diplomacy is the art of
saying 'Nice Doggy'
until you can find a rock."
 
--- Will Rodgers
}}</syntaxhighlight>
 
or <syntaxhighlight lang="j">text=: fread 'filename'</syntaxhighlight> if the text were stored in a file named <code>filename</code>.
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="sh">
<lang sh>
jq -nRr '[inputs] | reverse[]' input.txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 383 ⟶ 416:
The optional <pre>keep</pre> argument to <pre>readlines</pre> means to keep the newline '\n' char or '\r\n' digraph at the end of each line. The <pre>|></pre>
symbolism is the pipe operator. and the <pre>.|></pre> symbolism means to pipe each line in the read array to print separately.
<langsyntaxhighlight lang="julia">readlines("diplomacyrodgers.txt", keep=true) |> reverse .|> print</langsyntaxhighlight>{{output}}
<pre>
--- Will Rodgers
Line 399 ⟶ 432:
 
– there is enough memory to process the file in memory i.e to store the input file as a string, the sequence of lines, the reverse sequence of lines and the output file as a string.
<langsyntaxhighlight Nimlang="nim">import algorithm, strutils
 
proc reverseLines(infile, outfile: File) =
Line 414 ⟶ 447:
echo ">>>>> Output file:"
reverseLines(infile, stdout)
echo ">>>>>"</langsyntaxhighlight>
 
{{out}}
Line 433 ⟶ 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 438 ⟶ 488:
The following is an ISO-compliant <tt>program</tt>.
Some compilers, however, such as the FPC (Free Pascal Compiler) or Delphi, cannot handle files without file names.
<langsyntaxhighlight lang="pascal">program tac(input, output);
 
procedure reverse;
Line 483 ⟶ 533:
begin
reverse
end.</langsyntaxhighlight>
{{out}}
--- Will Rodgers
Line 494 ⟶ 544:
=={{header|Perl}}==
as one-liner ..
<langsyntaxhighlight lang="perl">// 20210803 Perl programming solution
 
< input.txt perl -e 'print reverse <>'</langsyntaxhighlight>
{{out}}
<pre>
Line 507 ⟶ 557:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 528 ⟶ 578:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
Obviously you can test the file handling by running the above, then changing eg Diplomacy to Diplomaxy and re-running it, and checking it outputs the previously saved c rather than the replacement x.
{{out}}
Line 541 ⟶ 591:
=={{header|Python}}==
Interactive program which takes input from a file :
<langsyntaxhighlight lang="python">
#Aamrun, 4th October 2021
 
Line 557 ⟶ 607:
 
[print(i) for i in fileData[::-1]]
</syntaxhighlight>
</lang>
Input file :
<pre>
Line 579 ⟶ 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}}==
<langsyntaxhighlight lang="rsplus">text <- scan("Rodgers.txt", character(), sep = "\n")
print(text)
reversed <- rev(text)
print(reversed)
write(reversed, "SaveTheOutput.txt")</langsyntaxhighlight>
{{out}}
<pre>Read 5 items
Line 610 ⟶ 684:
* May hold entire file in memory.
 
<syntaxhighlight lang="raku" perl6line>.put for reverse lines</langsyntaxhighlight>
 
===Few assumptions===
Line 621 ⟶ 695:
raku -e'print join "\x00\x00", (1..6).map: * x 8' > nul.txt
 
<syntaxhighlight lang="raku" perl6line>my $input-record-separator = "\x00\x00";
 
my $fh = open("nul.txt".IO, :r, :bin);
Line 644 ⟶ 718:
}
 
say $buffer; # emit any remaining record</langsyntaxhighlight>
{{out}}
<pre>66666666
Line 658 ⟶ 732:
 
No assumptions were made concerning line/record termination, &nbsp; as REXX takes care of that.
<langsyntaxhighlight lang="rexx">/*REXX pgm reads a file, and displays the lines (records) of the file in reverse order. */
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID=="," then iFID='REVERSEF.TXT' /*Not specified? Then use the default.*/
Line 669 ⟶ 743:
say @.k /*display a record of the file ──► term*/
end /*k*/
call lineout iFID /*close file, good programming practice*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 686 ⟶ 760:
::* &nbsp; CMS REXX compiler
::* &nbsp; CMS OREXX
<syntaxhighlight lang="text">/*REXX pgm reads a file, and displays the lines (records) of the file in reverse order. */
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID=="," then iFID='REVERSEF.TXT' /*Not specified? Then use the default.*/
Line 695 ⟶ 769:
say linein(iFID, j) /*display record contents ──► terminal.*/
end /*j*/
call lineout iFID /*close file, good programming practice*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 728 ⟶ 802:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 747 ⟶ 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."
<lang bash>tac rodgers.txt</lang>
saying 'Nice Doggy'
Output:
"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}}
<pre>
--- Will Rodgers
Line 759 ⟶ 855:
"Diplomacy is the art of
</pre>
 
Notice that '''tac''' is '''cat''' in reverse order.
 
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<langsyntaxhighlight ecmascriptlang="wren">import "./ioutil" for File, FileUtil
 
var fileName1 = "rodgers.txt"
Line 780 ⟶ 875:
}
// print contents of output file to terminal
System.print(File.read(fileName2))</langsyntaxhighlight>
 
{{out}}
Line 793 ⟶ 888:
=={{header|XPL0}}==
Usage: rev <will.txt
<langsyntaxhighlight XPL0lang="xpl0">char Array(1000, 1000); \(tacky)
int Line, Char, I;
def LF=$0A, EOF=$1A;
Line 809 ⟶ 904:
until Char = LF;
];
]</langsyntaxhighlight>
 
{{out}}
9,476

edits