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

From Rosetta Code
Content added Content deleted
(added Perl programming solution)
m (→‎{{header|REXX}}: added wording to the REXX section header.)
Line 89: Line 89:


=={{header|REXX}}==
=={{header|REXX}}==
No assumptions were made concerning line/record termination,   as REXX takes care of that.
<lang rexx>/*REXX pgm reads a file, and displays the lines (records) of the file in reverse order. */
<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.*/
parse arg iFID . /*obtain optional argument from the CL.*/

Revision as of 16:20, 3 August 2021

Reverse the order of lines in a text file while preserving the contents of each line is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Reverse the order of the lines (records) of an entire (input) file.

Show the results here, on this page.


For the input file, use the following five lines (records):

 "Diplomacy is the art of                                    ◄■■■■■■ starts in column 3.
   saying  'Nice Doggy'                                      ◄■■■■■■ starts in column 5,
until you can find a rock."                                  ◄■■■■■■ starts in column 2,
                                                             ◄■■■■■■ (a blank line),
                            --- Will Rodgers                 ◄■■■■■■ starts in column 30.


You can (or may) assume there are no trailing blanks,   and that line four has one blank.

Also, don't include the rightmost informative comments   (◄■■■■■■),   as they are not meant to be part of the file.

Nim

We provide a procedure which takes input and output files as parameters. Assumptions are the following:

– a line is a sequence of bytes terminated by CR, LF or CR-LF;

– 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. <lang Nim>import algorithm, strutils

proc reverseLines(infile, outfile: File) =

 let lines = infile.readAll().splitLines(keepEol = true)
 outfile.write reversed(lines).join("")

when isMainModule:

 let infile = open("reverse_file_lines.txt")
 echo ">>>>> Input file:"
 stdout.write infile.readAll()
 infile.setFilePos(0)
 echo ">>>>>"
 echo '\n'
 echo ">>>>> Output file:"
 reverseLines(infile, stdout)
 echo ">>>>>"</lang>
Output:
>>>>> Input file:
 "Diplomacy is the art of
   saying  'Nice Doggy'
until you can find a rock."

                            --- Will Rodgers
>>>>>


>>>>> Output file:
                            --- Will Rodgers

until you can find a rock."
   saying  'Nice Doggy'
 "Diplomacy is the art of
>>>>>

Perl

as one-liner .. <lang perl>// 20210803 Perl programming solution

< input.txt perl -e 'print reverse <>'</lang>

Output:
                            --- Will Rodgers

until you can find a rock."
   saying  'Nice Doggy'
 "Diplomacy is the art of

Raku

Simplest thing that could possibly satisfy the extremely vague task description and completely glossing over all of the questions raised on the discussion page.

ASSUMPTIONS:

  • File is redirected into STDIN from command line.
  • Is a Unix or Windows format text file.
  • Is in UTF8 encoding or some subset thereof.
  • May hold entire file in memory.

<lang perl6>.put for reverse lines</lang>

REXX

No assumptions were made concerning line/record termination,   as REXX takes care of that. <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.*/ call lineout iFid /*close file, good programming practice*/

                  do j=1  while lines(iFID)>0   /*read the file, one record at a time. */
                  @.j= linein(iFID)             /*assign contents of a record to array.*/
                  end   /*j*/

recs= j - 1 /*J will be 1 more ('cause of DO loop)*/

                  do k=recs  by -1  for recs    /*process array (@.k) in reverse order.*/
                  say @.k                       /*display a record of the file ──► term*/
                  end   /*k*/

call lineout iFid /*close file, good programming practice*/</lang>

output   when using the default input:
                             --- Will Rodgers

 until you can find a rock."
    saying  'Nice Doggy'
  "Diplomacy is the art of

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Input file lines:" + nl

fp = fopen("..\New\text.txt","r") r = "" txt = "" while isstring(r)

     r = fgetc(fp)
     if r = -1
        loop
     ok
     if r = char(10)
        txt += nl
     else
        txt += r
     ok

end

see txt + nl see "Reversed file lines: " + nl txt = str2list(txt) txt = reverse(txt) txt = list2str(txt) see txt fclose(fp)

see nl + "done..." + nl </lang>

Output:
working...
Input file lines:
Ring Programming Language #1
Ring Programming Language #2
Ring Programming Language #3

Reversed file lines: 
Ring Programming Language #3
Ring Programming Language #2
Ring Programming Language #1
done...