Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Content added Content deleted
mNo edit summary |
(Added Go) |
||
Line 20: | Line 20: | ||
Also, don't include the rightmost informative comments (◄■■■■■■), as they are not meant to be part of the file. |
Also, don't include the rightmost informative comments (◄■■■■■■), as they are not meant to be part of the file. |
||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<lang go>package main |
|||
import ( |
|||
"bytes" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"log" |
|||
"runtime" |
|||
) |
|||
func main() { |
|||
fileName1 := "rodgers.txt" |
|||
fileName2 := "rodgers_reversed.txt" |
|||
lineBreak := "\n" |
|||
if runtime.GOOS == "windows" { |
|||
lineBreak = "\r\n" |
|||
} |
|||
// read lines from input file |
|||
b, err := ioutil.ReadFile(fileName1) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
lines := bytes.Split(b, []byte(lineBreak)) |
|||
// remove final blank line, if any, added by some editors |
|||
if len(lines[len(lines)-1]) == 0 { |
|||
lines = lines[:len(lines)-1] |
|||
} |
|||
// write lines in reverse order to output file |
|||
for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 { |
|||
lines[i], lines[j] = lines[j], lines[i] |
|||
} |
|||
b = bytes.Join(lines, []byte(lineBreak)) |
|||
if err = ioutil.WriteFile(fileName2, b, 0o666); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
// print contents of output file to terminal |
|||
b, err = ioutil.ReadFile(fileName2) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
fmt.Println(string(b)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
--- Will Rodgers |
|||
until you can find a rock." |
|||
saying 'Nice Doggy' |
|||
"Diplomacy is the art of |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 32: | Line 87: | ||
"Diplomacy is the art of |
"Diplomacy is the art of |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |