Removing end of line character from file
When you create a text file, it is likely that the file is terminated with an end of line (EOL) character. In most cases this is what you expect, however there are some cases where the EOL character can be problematic.
For example, if you create a secret in Kubernetes from a file as we covered in the Argo CD app of apps blog, any EOL character in that file is encoded in the secret that is created and this may not be what you want.
Checking for EOL
You can check whether a line in a file is terminated with a EOL with the cat -e
command.
-e Display non-printing characters (see the -v option), and display a
dollar sign (‘$’) at the end of each line.
For example, let's create a text file.
echo hello > test.txt
Now we can inspect the EOL characters with cat -e
and we should see the line
ending with the character $ indicating there is an end of line character.
❯ cat -e test.txt
hello$
Removing EOL
We can remove this final character
truncate -s -1 test.txt
And now when we run cat -e
again we should the line ending with a ⏎
character indicating there is no end of line character.
❯ cat -e test.txt
hello⏎
EOL in Vim
In vim you can see whether there is an EOL character with the &endofline
option which will return 1 when there is an of line character and 0 when there is none.
:echo &endofline
A default vim configuration will show this in the status bar.
vim -u NONE test.txt
Which will show:
hello
~
"test.txt" [Incomplete last line] 1 line, 5 bytes
You can remove the end of line character in vim with the noendofline
option.
:set noendofline
After which vim will show:
hello
~
"test.txt" 1 line, 6 bytes
You can add the EOL character back with
:set endofline