Delete the last empty/blank line of the text file

Hi All,

I have a file.txt which seems like having three lines.

 
wc -l file.txt

3 file.txt

In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they did not work.

 
sed -i '/^ *$/d' file.txt > output.txt
awk 'NR > 1{print t} {t = $0}END{if (NF) print }' file.txt > output.txt
sed '${/^$/d}' file.txt > output.txt
sed '${/./!d}' file.txt > output.txt
sed '/^$/d' file.txt > output.txt
grep -v '^$' file.txt > output.txt

Thanks,

Please post the output of

od -c file.txt

using code tags. Replace confidential data, just in case, thanks. ANd which editor do you use?

I am using Text editor

 
od -c file.txt > out.txt
cat out.txt
 
0000000 : s i t e s : 3 1 6 5 \n 1 1 3 0
0000020 2 9 8 1 6 9 6 0 2 0 1 6 9 8
0000040 6 6 1 1 7 1 2 8 9 1 1 8 0 2
0000060 5 4 8 1 8 1 3 7 8 2 1 8 8 2
0000100 1 8 5 2 0 2 5 2 3 9 2 0 5 9
0000120 0 3 2 2 1 0 9 6 9 3 2 1 7 0
0000140 3 8 4 2 1 7 4 7 1 5 2 1 9 4
0000160 6 1 5 2 2 2 1 4 5 5 2 2 4 1
...
0072640 0 2 4 7 0 4 6 5 0 5 2 4 7 1
0072660 2 1 1 9 0 \n 0 . 7 1 4 4 8 7 3 
0072700 1 . 1 1 0 3 9 0 7 1 . 1 1 2 7
0072720 8 9 3 1 . 1 1 3 3 5 0 2 1 .
0072740 1 1 7 8 3 1 0 1 . 1 1 8 7 7 6
...
0202660 9 2 2 9 6 8 6 2 7 7 . 9 8 6 8
0202700 3 1 7 2 7 8 . 0 3 7 0 4 4 2 \n
0202720

That looks quite normal. Which text editor do you use? Did you try opening it with vi for example?

What does

awk '{print NF}' FS= file.txt

show?

I am using Notepad++ under Windows to see how the file looks like.

I tried the code that you suggested

awk '{print NF}' FS= file.txt > output.txt

> Edit with Notepad++ and it still shows the last line.

There is no need to redirect every output into a file. Just issue the awk line I posted and post the result here, thanks.
As said try another editor but notepad++. Maybe that is not the best choice for an editor to use (if you are allowed to use any other).

Try this...

tr -s '\n' input > output

--ahamed