Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Thundergnat (talk | contribs) m (→{{header|Raku}}: typo) |
m (changed wording of the (draft) task, added a requirement to show) |
||
Line 1: | Line 1: | ||
{{Draft task}} |
{{Draft task}} |
||
;Task: |
|||
;Task:Reverse lines of entire file. |
|||
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. |
|||
<br><br> |
<br><br> |
||
Revision as of 15:46, 3 August 2021
- 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.
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>
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...