Delete the last empty/blank line of the text file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete the last empty/blank line of the text file
# 1  
Delete the last empty/blank line of the text file

Hi All,

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

Code:
 
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.


Code:
 
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,
# 2  
Please post the output of
Code:
od -c file.txt

using code tags. Replace confidential data, just in case, thanks. ANd which editor do you use?
# 3  
I am using Text editor

Code:
 
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


Last edited by zaxxon; 10-24-2011 at 12:08 PM.. Reason: correcting code tags
# 4  
That looks quite normal. Which text editor do you use? Did you try opening it with vi for example?

What does
Code:
awk '{print NF}' FS= file.txt

show?
# 5  
I am using Notepad++ under Windows to see how the file looks like.

I tried the code that you suggested

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

> Edit with Notepad++ and it still shows the last line.
# 6  
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).
# 7  
Try this...
Code:
tr -s '\n' input > output

--ahamed
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #293
Difficulty: Easy
Linux is the most popular operating system among supercomputers today (2019).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete everything after empty line in file

Hello, I am trying to remove everything coming after empty line under ubuntu14.04. How may I do it? file: aaa 020390 bb00id3 c03wisi 209dl x092sia 0290s I expect: aaa 020390 bb00id3 c03wisi (3 Replies)
Discussion started by: baris35
3 Replies

2. Shell Programming and Scripting

Tried many options but unable to delete blank lines from text file

Hi, I tried the following options but was unable to delete blank lines from file Input file = temp.hash.txt temp.hash.txt content 90 0 89.56 0 0 57575.4544 56.89 (9 Replies)
Discussion started by: uuuunnnn
9 Replies

3. Shell Programming and Scripting

Delete blank line in regular file

Hi all, I have file1 with line blank e.g. $cat file1 aaa 111 222 333 444 bbb 555 666 777 888 ccc ddd 1010 1010 1010 eee then i need delete the lines blank (3 and 5) so show $cat file1 aaa 111 222 333 444 bbb 555 666 777 888 ddd 1010 1010 1010 (5 Replies)
Discussion started by: aav1307
5 Replies

4. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

5. Shell Programming and Scripting

delete blank line from middle of a file

All, I have a file which contains two entry with spaces (either one or more than one space). ex. /tmp/scripts/sql CUST_YR_END_INI.sql /tmp/scripts/sql CUST_WK_END_INI.sql /tmp/scripts/sql CUST_MTH_END_INI.sql /tmp/scripts/sql CUST_YR_END_INC.sql now I want to... (11 Replies)
Discussion started by: anshu ranjan
11 Replies

6. Shell Programming and Scripting

how to scan for the empty or blank line

I am having a text file sample.txt The contents of the sample.txt is as follows. ____________ aaa,bbb ccc,ddd ____________ first line contains aaa,bbb second line is blank third line contains ccc,ddd Now I need to write a script to search in the file sample.txt. If it contains... (4 Replies)
Discussion started by: kmanivan82
4 Replies

7. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

8. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

9. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies

10. Shell Programming and Scripting

how to get rid of blank line in a flat text file

Hi, I have a flat text file which contains blank line between each text line. Is there any command to get rid of it? Thanks for your help (11 Replies)
Discussion started by: xfang
11 Replies

Featured Tech Videos