File line operations

Hello, How to perform the following file manipulations in Deno ? - Read the nth line - Remove the nth line - Add a line before the nth line nth can he anything including and between first and last. Thanks
There’s no specific API for these methods
Assuming that performance is not your primary concern and you are familiar with JavaScript arrays, the docs should give you enough information You can, for example, split the file content on the new line character \n and modify the nth item in the array
To mutate and array and insert a new line you can use splice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Array.prototype.splice() - JavaScript | MDN
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().
I’d probably create a function that reads a file line by line and writes it down to a new file. It takes a function as an argument and executes it when it gets to the desired range. Then writes the rest of the file after the range and renames it to the old one deleting the old one. There might be a better way. But that’s how I’d solve your problem.
I see, so there's no other way but to process the whole file anyway. Thanks
UUUnknown User11/4/2022
2 Messages Not Public
Sign In & Join Server To View
I mean yes I can choose not to read it the whole file at once but I will still need to read the whole file in the end, even if in chunks.
I wrote a blog post on processing CSV data, and here is the section where I show how to use a TextProtoReader to read a file one line at a time: https://deno-blog.com/Processing_CSV_files_with_Deno.2022-09-20#reading-data-from-a-large-csv-file
Craig's Deno Diary
Craig's Deno Diary is a blog that covers the JavaScript and TypeScript runtime Deno and focuses on how to write Deno programs and use Deno libraries.
Of course, but it still requires looping through all lines when it comes to inserting and deleting.
Depending on what's stored in the files, you could opt for a database instead which offers insertion, deletion and replacement of entries. Or you could opt for smaller files. Breaking the one thing up over many files.

Looking for more? Join the community!

Recommended Posts
FFI how to pass (or get) 128-bit bigints to (or from) Rust functions?I want to pass bigints from Deno and get i128 or u128 in Rust. Do I have to use typed arrays for thaUnable to view logs to debug edge function for Deno Fresh + Deno DeployThanks for making Deno, and Deploy, and Fresh. I'm a huge fan. I'm building a feature to allow userTypeScript enumsSo enums are objects with all keys being values and values being keys. Most of the time when using eHow to get complete Deno RAM usage?Do we use `Deno.memoryUsage().rss` or should it be `rss+heapUsed+external`?Deno NPM VSCode IntellisenseIs there a work around for this at the moment? I want to try out Deno's NPM support but I lose IntelNeed help making fresh, rutt and preact base path awareI'm currently trying to make fresh base path aware. I already patched rutt to work with base paths. How do I render react?I'm using the configuration as shown in the Deno manual with preact, but how would I server-side renResolving 'dynamically imported module evaluation pending, but no pending ops'I've been getting this error quite a lot, but I haven't found a single way to know *which* unresolvehttp serve and serveTls using async iterator cannot serve multiple domains```javascript async f_serveTls() { var o_self = this // check if ssl cert existsHow to test if a file was created?I'm making a deno library that creates some folders and files, how can i check if it generates then Deno bundle duplicating variablesUsing Deno 1.26.2 I am trying to bundle https://github.com/Savory/Danet-Starter using `deno bundle Simple test, lots of type errorsHi! I made a library for Deno (https://github.com/Industrial/fresh-emotion) and am now trying to adAm I closing this server wrongly? The port is not free for a new listener! (`AddrInUse`)```ts const server = Deno.listen({ port: 80 }); (async function(){ for await(const conn of serSync uuid from stringHow can I generate a UUID from a string synchronously? In the standard library I only found this asVS Code, Hello World -- F5 shows nothing in console?I have a simple hello world program: ```ts let message: string = 'Hello World'; console.log(messageHow to transform a Readable Stream to a file.I have a ReadableStream from a post request and i cant find a way to transform the body(ReadableStreSilence error logs on Oak serverI'm trying to silence specific errors on an Oak server on Deploy. A Github issue said to add an 'errAre pointers supported in Fast API return types?If no, how do I return pointers?Reddit fetch Vs. My noob brain part. IHello! So I want to fetch Reddit posts. I use this library. I have a few questions. https://github.cHow do I detect mouse clicks and key presses with Deno.stdin?I don't even really have any base code, so I was just wondering as to how do I do the problem specif