Skip to content

How to read remote text file and write it in reverse line order? #1993

Answered by szmarczak
mariusa asked this question in Q&A
Discussion options

You must be logged in to vote

This is a problem totally unrelated to Got, best if asked on StackOverflow :)

Writing in reverse order is highly inefficient. Instead, read it in reverse.

You can write offests of the line breaks (you can use a TransformStream). For example the offsets for

line 1
long line 2
line 3
long line 4
another line 5

are [6, 18, 25, 37]. Once the file is saved on disk, you can just pass start and end arguments in https://nodejs.org/api/fs.html#fscreatereadstreampath-options

start: 37 + 1 (line break) = 38, end: Infinity
start: 25 + 1 (line break) = 26, end: 37
start: 18 + 1 (line break) = 19, end: 25
start: 6 + 1 (line break) = 7, end: 18
start: 0, end: 6

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mariusa
Comment options

Answer selected by szmarczak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants