NOTE: this question used to be worded differently, using “with/out newline” instead of “with/out empty line”
I have two files, one with an empty line and one without:
File: text_without_empty_line
$root@kali:/home#cat text_without_empty_line
This is a Testfile
This file does not contain an empty line at the end
$root@kali:/home#
File: text_with_empty_line
$root@kali:/home#cat text_with_empty_line
This is a Testfile
This file does contain an empty line at the end
$root@kali:/home#
Is there a command or function to check if a file has an empty line at the end? I already found this solution, but it does not work for me. (EDIT: IGNORE: A solution with preg_match and PHP would be fine as well.)
I found the solution here.
IMPORTANT: Make sure that you have the right to execute and read the script!
chmod 555 script
USAGE:
In bash:
As a shell script that you can call (paste it into a file,
chmod +x <filename>
to make it executable):The
\Z
meta-character means the absolute end of the string.So here you are looking at a new line at the absolute end of the string.
file_get_contents
will not add anything at the end. BUT it will load the entire file into memory; if your file is not too big, its okay, otherwise you'll have to bring a new solution to your problem.Just type:
If there is a newline it will end with
$
symbol. If not, it will end with a%
symbol.