admin管理员组文章数量:1289867
What's the best way to overwrite a line in a large (2MB+) text file using node.js?
My current method involves
- copying the entire file into a buffer.
- Spliting the buffer into an array by the new line character (
\n
). - Overwriting the line by using the buffer index.
- Then overwriting the file with the buffer after join with
\n
.
What's the best way to overwrite a line in a large (2MB+) text file using node.js?
My current method involves
- copying the entire file into a buffer.
- Spliting the buffer into an array by the new line character (
\n
). - Overwriting the line by using the buffer index.
- Then overwriting the file with the buffer after join with
\n
.
-
fs.write()
gives you the ability to write to a specific offset/position, do you know that index ahead of time? – Dominic Barnes Commented Jul 27, 2012 at 17:55 - Not really. How can you pute the offset of each line? – Larry Battle Commented Jul 27, 2012 at 18:47
- I figured it was worth a shot, sometimes you have a specified format for a file, which makes calculations like that possible. – Dominic Barnes Commented Jul 27, 2012 at 20:32
- If you use fixed-length lines in your text file the offset approach might work...but at that point I would reconsider the approach. Text files are great if you only need to append them. After that I would consider a database. – Hector Correa Commented Jul 27, 2012 at 20:49
3 Answers
Reset to default 4First, you need to search where the line starts and where it ends. Next you need to use a function for replacing the line. I have the solution for the first part using one of my libraries: Node-BufferedReader.
var lineToReplace = "your_line_to_replace";
var startLineOffset = 0;
var endLineOffset = 0;
new BufferedReader ("your_file", { encoding: "utf8" })
.on ("error", function (error){
console.log (error);
})
.on ("line", function (line, byteOffset){
startLineOffset = endLineOffset;
endLineOffset = byteOffset - 1; //byteOffset is the offset of the NEXT byte. -1 if it's the end of the file, if that's the case, endLineOffset = <the file size>
if (line === lineToReplace ){
console.log ("start: " + startLineOffset + ", end: " + endLineOffset +
", length: " + (endLineOffset - startLineOffset));
this.interrupt (); //interrupts the reading and finishes
}
})
.read ();
Maybe you can try the package replace-in-file
suppose we have a txt file as below
// file.txt
"line1"
"line2"
"line5"
"line6"
"line1"
"line2"
"line5"
"line6"
and we want to replace:
line1 -> line3
line2 -> line4
Then, we can do it like this:
const replace = require('replace-in-file');
const options = {
files: "./file.txt",
from: [/line1/g, /line2/g],
to: ["line3", "line4"]
};
replace(options)
.then(result => {
console.log("Replacement results: ",result);
})
.catch(error => {
console.log(error);
});
the result as below:
// file.txt
"line3"
"line4"
"line5"
"line6"
"line3"
"line4"
"line5"
"line6"
More details please refer to its docs: https://www.npmjs./package/replace-in-file
This isn't file size focoused solution, but Overwrites a line in a file using node.js
. It may help other people that search engines redirect to this post, like me.
import * as fs from 'fs'
const filename = process.argv[2]
const lineIndexToUpdate = parseInt(process.argv[3]) - 1
const textUpdate = process.argv[4]
function filterLine(indexToUpdate, dataString) {
return dataString
.split('\n')
.map((val, index) => {
if (index === indexToUpdate)
return textUpdate
else
return val
})
.join('\n')
}
fs.readFile(filename, 'utf8', (err, data) => {
if (err) throw err
fs.writeFile(filename, filterLine(lineIndexToUpdate, data), (err, data) => {
if (err) throw err
console.log("Line removed")
})
})
Script use exemple:
node update_line.js file 10 "te voglio benne"
本文标签: javascriptOverwrite a line in a file using nodejsStack Overflow
版权声明:本文标题:javascript - Overwrite a line in a file using node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741436855a2378685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论