Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing last blank line

Tags:

There is a para break at the end of my .csv file. I tried to remove the blank line that is at the end of the file using the following command.

sed -i '/^$/d' combined.csv 

But it does not work and a blank line is still there. I can remove the last line using the following command.

sed -i '$d' combined.csv 

But is it possible to check if the last line is really empty before removing it?

Update:

I am using the following command to check if each line start with a number.

sed -i '1s/^[^0-9]*//' combined.csv 

This checks only for the first line and not the rest of the lines. How do I make it check all the lines in the file? This might solve my problem.

like image 466
shantanuo Avatar asked Dec 15 '10 10:12

shantanuo


People also ask

How do I remove blank lines?

There is also a very handy keyboard shortcut to delete rows (columns or cells). Press Ctrl + – on the keyboard. That's it! Our blank rows are gone now.

How do I remove blank lines between lines in Word?

To start removing empty lines, open your document with Microsoft Word. Click “Home” in the menu and then select “Replace” on the right of the screen. Then click “Replace All” at the bottom of the window. After you click, all the blank lines will be removed from your document.


1 Answers

Try ${/^$/d;} this will only match an empty line if it is the last line of the file.

Update: for your second question, just remove the 1 before the s, i.e.: sed -i 's/^[^0-9]*//' combined.csv

like image 171
Bart Sas Avatar answered Sep 21 '22 20:09

Bart Sas