fs.readFileSync adds \r to the end of each string - node.js

I'm using let names = fs.readFileSync(namefile).toString().split("\n"). Whenever I do
for(const name of names) {
console.log(`First Name: ${name.split(" ")[0]} Last Name: ${name.split(" ")[1]}
}
the last name part always has \r at the end, how do I make it not add the \r?

fs.readFileSync doesn't add anything to the end of lines,
instead the file that you're trying to read is using CRLF line endings, meaning that each line ends with the \r\n sequence.
Really your file looks something like this:
line1\r\nline2\r\nline3\r\n
But your text editor will hide these characters from you.
There are two different ways you can fix this problem.
Change the type of line endings used in your text editor
This is IDE specific but if you use Visual Studio Code you can find the option in the bottom right.
Clicking on it will allow you to change to LF line endings, a sequence where lines are followed by a single \n character.
Replace unwanted \r characters
Following on from your example we can use .replace to remove any \r characters.
let names = fs.readFileSync(namefile)
.toString()
.replace(/\r/g, "")
.split("\n")
More on line endings

Related

How to detect what kind of break line in a text file in python?

My problem is the following. I have a text file with a bunch of lines in it. The problem is this text might have been created by Windows or Unix or Mac.
I want to open this text in python (as a string block) and split according to a break line to get an array at the end with all lines. The problem is I only tested this with a windows created file so I can split the string block easily according \n. But if I understand correctly other environnement use \r \r\n ...Etc
I want a general solution where I can detect what kind of line break is used in a file before I start splitting in order to split it correctly. Is that possible to do?
thanks;
UNIX_NEWLINE = '\n'
WINDOWS_NEWLINE = '\r\n'
MAC_NEWLINE = '\r'
This will be how the different os apply line breaks in a file and how python sees it

remove all empty lines from text files while keeping format

I have multiple notepad text files which contains one empty line (the last line of each file). I want to delete the empty line form all files. I tried different grep and awk lines but they didn't work plus they messed up the file format; all text are shown on one line instead of separate line. i also tried with notepad++ regex to find ^\s*$ and replace it with nothing, but it also didn't work.
Current text file looks like this:
apples
oranges
peaches
[empty line]
The output should be
apples
oranges
peaches
Ctrl+H
Find what: \R^$
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
Replace all
Explanation:
\R : any kind of linebreak
^ : begining of line
$ : end of line
Result for given example:
apples
oranges
peaches
If you want to delete the last line of the file, use sed '$d'. If you want to do that only when the last line is empty, use sed '${/^$/d;}' (This treats a line with some whitespace as a non-blank line, so you might prefer sed '${/^ *$/d;}' or some variant.
The "empty last line" may be a matter of interpretation. From the wikipedia "Newline" article:
Two ways to view newlines, both of which are self-consistent, are that newlines either separate lines or that they terminate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not terminated by a newline. On the other hand, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. Conversely, if a newline is considered a terminator, all text lines including the last are expected to be terminated by a newline. If the final character sequence in a text file is not a newline, the final line of the file may be considered to be an improper or incomplete text line, or the file may be considered to be improperly truncated.
In my little world, the Visual Studio Code editor takes the former view; vim the latter.

Vim: how to visual select up to characters with backspaces in

I have a *.qif file (text file) in which I would like to remove all lines from the second line (not including the header) to a line which contains the characters 'D1/7/2015'. In vim I have tried block selecting from the second line and searching up to a the characters but it doesn't work. What I have tried so far:
Move the cursor to second line then:
v/D1/7/2015
v/D1//7//2015
v/D1///7///2015
None of these seem to work.
You can escape slashes with backslashes:
v/D1\/7\/2015
or tell Vim to parse your pattern with "verymagic" syntax:
v/\vD1/7/2015
See :help \v.
Not exactly what I wanted but it works. I used /D1.7.2015 to get to the line then started visual selection up to the beginning of the file with vgg after which I moved one line down with the cursor then pressed d.
You can delete directly without using the visual selection from the second line to the line containing 'D1/7/2015' by executing the command below:
:2,/D1\/7\/2015/ d

Replace CR by CR LF

I'm on Windows and I have an odd text file containing mostly CR+LF line ending. A few lines end with only CR. Which tool to use to transform these odd lines into well formatted (e.g. CR+LF terminated) lines?
I could use either GnuWin32 tools or Python to solve this.
The main problem I have is that I cannot open the file as text file since Python (as most other text processors, such as awk) don't recognize the mixed line endings. So I believe the solution must incorporate binary processing of the file.
The again, I cannot just replace CR by CR LF, since there are also CR LF line endings existing that must not be touched.
To replace lines you can use regular expressions:
\r+ to find CR
\r\n is the text you want as replacement text.
Regular Expressions in Python:
Regular Expression
import re
txt='text where you want to replace the linebreak'
out = re.sub("\r+", '\r\n', txt)
print out

Can you force Vim to show a blank line at the end of a file?

When I open a text file in Notepad, it shows a blank line if there is a carriage return at the end of the last line containing text. However, in Vim it does not show this blank line. Another thing I've noticed is that the Vim editor adds a carriage return to the last line by default (even though it doesn't show it). I can tell, because if I open a file in Notepad that was created in Vim, it shows a blank line at the end of the file.
Anyway, I can live with these two differences, but I'm wondering if there is an option in Vim that allows you to toggle this behaviour.
Thanks
PS - GVim 7.2
[Update]
Would this make sense to be on Server Fault instead?
[Update 2]
I'll rephrase this... I need to know when there is a carriage return at the end of single line file (Notepad shows an extra line with no text, with Vim I cannot tell). This is due to a Progress program that reads a text file (expects a single line, but with a carriage return) and parses the text for some purpose. If there is no carriage return, Progress treats the line as if it is null.
[Workaround Solution]
One way I've found to ensure there is a carriage return (but make sure I don't add a second one) is to make sure I have the end of line write option turned on (:set eol) and then just do a write/save. This will put an end of line in the file if it's not already there. Otherwise, it doesn't add a new one.
:help endofline
explains how you could stop vim from adding an extra newline.
It seems that vim treats newline as a line terminator, while notepad treats it as a line separator: from http://en.wikipedia.org/wiki/Newline
There is also some confusion whether
newlines terminate or separate lines.
If a newline is considered a
separator, there will be no newline
after the last line of a file. The
general convention on most systems is
to add a newline even after the last
line, i.e., to treat newline as a line
terminator. Some programs have
problems processing the last line of a
file if it isn't newline terminated.
Conversely, programs that expect
newline to be used as a separator will
interpret a final newline as starting
a new (empty) line. This can result in
a different line count being reported
for the file, but is otherwise
generally harmless.
If I recall correctly, on unix-y systems a text file must be terminated with a newline.
One useful Vim option is
set list
It will help you see all end of lines characters (and possibly other generally invisible chars). So you will be able to view this last endofline directly in Vim and not only in Notepad.
When you open the file in VIM the status line should say [noeol] after the filename. So that's one indication. As Manni said, you can change this by setting both the endofline option off and the binary option on. You can set this as your default settings in a .vimrc file.

Resources