Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Reverse the order of lines in a text file while preserving the contents of each line (view source)
Revision as of 12:43, 28 August 2022
, 1 year agosyntax highlighting fixup automation
(Realize in F#) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25:
{{trans|Python}}
<
V fileData = File(:argv[1]).read().split("\n")
L(line) reversed(fileData)
print(line)</
=={{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.
<
DEFINE BUFFERSIZE="1000"
BYTE ARRAY buffer(BUFFERSIZE)
Line 87:
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</
{{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}}==
<
with Ada.Containers.Indefinite_Vectors;
with Ada.Command_Line;
Line 136:
end loop;
end Reverse_Lines_In_File;</
=={{header|ALGOL W}}==
<
% 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.</
{{out}}
<pre>
Line 175:
</pre>
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 197:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<
open (source, "text.txt")
textEnt$ = ""
Line 213:
next n
close source
end</
{{out}}
<pre>
Line 221:
=={{header|F_Sharp|F#}}==
<
// 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>
Line 235:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
"rodgers.txt" utf8 file-lines <reversed> [ print ] each</
{{out}}
<pre>
Line 251:
=={{header|FreeBASIC}}==
<
dim as string textEnt, textSal()
dim as integer n, linea = 0
Line 266:
next n
close #1
sleep</
{{out}}
<pre>
Line 277:
=={{header|Free Pascal}}==
<
{$IFDEF FPC}
{$MODE DELPHI}
Line 300:
end;
writeln(Sl.text);
end.</
<pre> --- Will Rodgers
Line 309:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 351:
}
fmt.Println(string(b))
}</
{{out}}
Line 363:
=={{header|Haskell}}==
<
import qualified Data.Text.IO as TIO
main :: IO ()
main = TIO.interact $ T.unlines . reverse . T.lines</
{{out}}
<pre>
Line 380:
=={{header|J}}==
<
--- Will Rodgers
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of</
where
<
"Diplomacy is the art of
saying 'Nice Doggy'
Line 394:
--- Will Rodgers
}}</
or <
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="sh">
jq -nRr '[inputs] | reverse[]' input.txt
</syntaxhighlight>
{{out}}
<pre>
Line 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.
<
<pre>
--- Will Rodgers
Line 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.
<
proc reverseLines(infile, outfile: File) =
Line 447:
echo ">>>>> Output file:"
reverseLines(infile, stdout)
echo ">>>>>"</
{{out}}
Line 471:
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.
<
procedure reverse;
Line 516:
begin
reverse
end.</
{{out}}
--- Will Rodgers
Line 527:
=={{header|Perl}}==
as one-liner ..
<
< input.txt perl -e 'print reverse <>'</
{{out}}
<pre>
Line 540:
=={{header|Phix}}==
<!--<
<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 561:
<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>
<!--</
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 574:
=={{header|Python}}==
Interactive program which takes input from a file :
<
#Aamrun, 4th October 2021
Line 590:
[print(i) for i in fileData[::-1]]
</syntaxhighlight>
Input file :
<pre>
Line 613:
</pre>
=={{header|R}}==
<
print(text)
reversed <- rev(text)
print(reversed)
write(reversed, "SaveTheOutput.txt")</
{{out}}
<pre>Read 5 items
Line 643:
* May hold entire file in memory.
<syntaxhighlight lang="raku"
===Few assumptions===
Line 654:
raku -e'print join "\x00\x00", (1..6).map: * x 8' > nul.txt
<syntaxhighlight lang="raku"
my $fh = open("nul.txt".IO, :r, :bin);
Line 677:
}
say $buffer; # emit any remaining record</
{{out}}
<pre>66666666
Line 691:
No assumptions were made concerning line/record termination, as REXX takes care of that.
<
parse arg iFID . /*obtain optional argument from the CL.*/
if iFID=='' | iFID=="," then iFID='REVERSEF.TXT' /*Not specified? Then use the default.*/
Line 702:
say @.k /*display a record of the file ──► term*/
end /*k*/
call lineout iFID /*close file, good programming practice*/</
{{out|output|text= when using the default input:}}
<pre>
Line 719:
::* CMS REXX compiler
::* 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 728:
say linein(iFID, j) /*display record contents ──► terminal.*/
end /*j*/
call lineout iFID /*close file, good programming practice*/</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 761:
see nl + "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 783:
{{works with|Bourne Again SHell}}
<syntaxhighlight lang
Output:
<pre>
Line 797:
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<
var fileName1 = "rodgers.txt"
Line 813:
}
// print contents of output file to terminal
System.print(File.read(fileName2))</
{{out}}
Line 826:
=={{header|XPL0}}==
Usage: rev <will.txt
<
int Line, Char, I;
def LF=$0A, EOF=$1A;
Line 842:
until Char = LF;
];
]</
{{out}}
|