Nodejs line-by-line close calling next line? - node.js

I'm using the plugin line-by-line to read a very large file. There's a case where I want just the first line, so I'd close the connection immediately in that case.
However, I was noticing that it would try to process the next line regardless. I dumbed it down as far as I could, and wrote this:
lr.on("line", function (line) {
lr.pause();
console.log("\rLine");
lr.close();
}
My console shows:
Line
Line
Without the lr.close(), it only logs Line once.
What am I missing?

Take a look to Source
It print last lineFragment for you.
If you don't need it - call lr.end();.

Related

How to use Node.js streams to append to end of file?

I have a node.js app, which opens a stream:
outputStream = fs.createWriteStream("output.txt");
I then, asynchronously, add text to the file:
outputStream.write( outputTxt, "utf8" );
This code is being run inside a loop, so it happens hundreds of times. However, the loop is asynchronous, so it sometimes pauses, and I can edit the output.txt file in an external editor in the meantime, and (for example) add a few chars in the beginning.
However, when I do that, the next time the outputStream.write is executed, it overwrites the last few chars previously added (the same number of chars that I added externally).
Is there some way to prevent this? Some way to tell the writeStream find the end of the file and then add the text?

file.txt content disappearing after calling the next function

I am trying to get my program to write over a simple config.txt file.
Which is actually working: it writes the new content over the old one if one condition is met and proceeds to the next block.
However, when the next block calls another function of my program, the content of the file simply dissapears: the .txt file becomes empty.
I have searched SO for similar issues but couldn't find any posts.
I looked into:
How to search and replace text in a file using Python?
Search and replace a line in a file in Python
And and got some of these to work but always ran into the issue above.
Please be gentle as I am a newbie and this is my first "not Hello_world" program and my first post. Thanks:)
>>>Content of the default config.txt file is 'runcount=0'
def alias(confirmation):
###First block###
if confirmation.lower()=='yes':
print("...")
config=open('config.txt','r')
data=config.read()
config.close()
newdata=data.replace('runcount=0','runcount=1')
config=open('config.txt','w')
config.write(newdata)
config.close()
confirmation2=input()
>>>Content of the config.txt file is now 'runcount=1'
###Second block###
if confirmation2.lower()=='yes':
print("...")
return main()
else:
...
>>>The main() function is called... However the config.txt file is now empty
I choose to show you this code because I think it is the most readable.
I tried the "with" method which gives me the same results.
I also tried to write to a new file, remove the default and rename the new to config.txt... same issue.
I don't get why the file becomes empty as I closed it.
No error msg, my program just goes on but the file is empty.
Please note that I used open(config.txt, 'r') in another function of my program, only once yet.
It happens that I coded the following line in main() and did not close config.txt:
config=open("config.txt","w+")
And forgot about it...
Thank you guys for the hint.
I'm going to hide somewhere very dark now.

Attempting to append all content into file, last iteration is the only one filling text document

I'm trying to Create a file and append all the content being calculated into that file, but when I run the script the very last iteration is written inside the file and nothing else.
My code is on pastebin, it's too long, and I feel like you would have to see exactly how the iteration is happening.
Try to summarize it, Go through an array of model numbers, if the model number matches call the function that calculates that MAC_ADDRESS, when done calculating store all the content inside a the file.
I have tried two possible routes and both have failed, giving the same result. There is no error in the code (it runs) but it just doesn't store the content into the file properly there should be 97 different APs and it's storing only 1.
The difference between the first and second attempt,
1 attempt) I open/create file in the beginning of the script and close at the very end.
2 attempt) I open/create file and close per-iteration.
First Attempt:
https://pastebin.com/jCpLGMCK
#Beginning of code
File = open("All_Possibilities.txt", "a+")
#End of code
File.close()
Second Attempt:
https://pastebin.com/cVrXQaAT
#Per function
File = open("All_Possibilities.txt", "a+")
#per function
File.close()
If I'm not suppose to reference other websites, please let me know and I'll just paste the code in his post.
Rather than close(), please use with:
with open('All_Possibilities.txt', 'a') as file_out:
file_out.write('some text\n')
The documentation explains that you don't need + to append writes to a file.
You may want to add some debugging console print() statements, or use a debugger like pdb, to verify that the write() statement actually ran, and that the variable you were writing actually contained the text you thought it did.
You have several loops that could be a one-liner using readlines().
Please do this:
$ pip install flake8
$ flake8 *.py
That is, please run the flake8 lint utility against your source code,
and follow the advice that it offers you.
In particular, it would be much better to name your identifier file than to name it File.
The initial capital letter means something to humans reading your code -- it is
used when naming classes, rather than local variables. Good luck!

tail -f implementation in node.js

I have created an implementation of tail -f in node.js using socket.io and fs.watch function.
I read the file using fs.readFile, convert it into array of lines and returns it to the client. Stores the current length in variable.
Then whenever the "file changed" event fires, I re-read the whole file, converts it into array of lines. And then compare the old length and current length. and slice it like
fileContent.slice(oldLength, fileContent.length)
this gives me the changed content. So running perfectly fine.
Problem: I am reading the whole file every time the file gets changed, which is not efficient if file is too large. So is there any way, of reading a file once, and then gets the changed content if there is any change?
I have also tried, spawning child process for "tail -f"
var spawn = require ('child_process').spawn;
var child = spawn ('tail', ['-f', logfile]);
child.stdout.on ('data', function (data){
linesArray = data.toString().split("\n")
console.log ( "Data sent" + linesArray[0]);
io.emit('changed', {
data: linesArray,
});
});
the problem with this is:
on("data") event fires multiple time when I save the logfile by writing some content.
On first load, it correctly returns the last ten line of the file. But if there is a change then it return the whole content again and again.
So if you have any idea of solving this problem let me know. Till then I will dig the internet.
So, I got the solution by reading someone else's code. So solution was to use fs.open which will open the file and then instead of reading whole file we can read the particular block from the file using fs.read() function.
To know about the fs.open/fs.read, read this nodejs-file-system.
Official doc : fs.read

Pulling lines out of a file Matlab

I want to pull every line in a file that has a certain word in it and format it a certain way using matlab. I have started to do this but all my code keeps doing is pulling out the first line and repeating it over and over again. It won't move onto any other line. I dont see how I would fix this. Here is my code.
fid=fopen('suspiciousfile.txt');
myLine=fgetl(fid);
countline=0;
while ischar(myLine)
strfind('Drexel', myLine)
countline=countline+1;
fprintf('Line #%d %s.\n', countline,myLine);
end
I assume you are after something like this:
fid=fopen('suspiciousfile.txt');
countline=0;
myLine=fgetl(fid);
while ischar(myLine)
countline=countline+1;
if strfind(myLine, 'Drexel')
fprintf(1,'Line #%d %s.\n', countline,myLine);
end
myLine=fgetl(fid);
end
fclose(fid);
A few notes:
You need to call fgetl within your loop so that you continue reading new lines.
The arguments to strfind(TEXT,PATTERN) where you search for PATTERN in TEXT
If you only want to print certain lines, you need an if statement.
It is good practice to close any file you open with fclose when you are done with it.

Resources