0

I have some data in a .txt file like so:

Michael|30
John|23
Eric|34
Dave|20

I want to select a random line from that file, then delete the entire line from that file and write it to another.

I have tried different ways but none of them seem to work properly.

So any ideas how to actually do it?

TGrif
  • 5,725
  • 9
  • 31
  • 52
GeorgeDG
  • 7
  • 4
  • Generally you can't delete a line from a file (or a character, for that matter) because file systems don't typically support files that contain gaps. What you probably want to do is copy the content EXCEPT for the line to a temporary file. Output the single line you want to to the other file. Then when you're done, delete the original and rename the temporary file with the original file's name. – tvanfosson Mar 17 '18 at 20:41
  • I assume you've looked over the [Node File System docs](https://nodejs.org/dist/latest-v8.x/docs/api/fs.html). It explains how to read and write from files. If so, it is customary to give a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your specific problem. – David784 Mar 17 '18 at 20:41
  • related: https://stackoverflow.com/questions/46483326/is-there-a-more-elegant-way-to-read-then-write-the-same-file-with-node-js-stre – Jonas Wilms Mar 17 '18 at 21:03
  • _I have tried different ways_. Which ones ? – TGrif Mar 17 '18 at 23:27
  • Oh my god, I just noticed my spelling mistakes, thanks for correcting my post, that's embarrassing So this is what I have to delete the first file, but I have no idea how to write it to another... [link](https://pastebin.com/AWVafN6w) – GeorgeDG Mar 19 '18 at 00:05

0 Answers0