I have this TXT file that is created by a bash script and at the end I want to remove the last line of that file that is an empty one.head --lines=-1 r3.txt > r4.txtsed '$d' r3.txt > r4.txtcat r3.txt | head -n -1  > r4.txtr4.txt continues to have the last empty line...I am on a linux CentOS 6.x server.Does this have anything to do with line endings (unix x windows)?Any clues?
removing the last line of a file not working?
sed;cat;head
Probably there is no empty line at the end.Unix text files consist of lines, each of which ends in a line feed character ('\n', ^J, 0x0a). That is, the linefeed character is not a line separator, but a line ending character. This is especially also true for the last line, and therefore for the file as a whole.For example, a text file containingabcdefwill look like this when looked at with xxd:0000000: 6162 630a 6465 660a                      abc.def.You can clearly see the single 0a at the end. On the other hand, if there's an additional an empty line at the end, you get0000000: 6162 630a 6465 660a 0a                   abc.def..Note that now there are two 0a bytes.You can also see this with wc -l which counts lines. On the first file, it will give 2, on the second file it will give 3. Note also that if the final line feed is missing (which means the file is not actually a valid text file, although most tools will be able to treat it anyway), wc will only count one line.