Remove last blank line of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last blank line of file
# 1  
Old 04-08-2009
Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank,

I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file.

Is there a way I can recursively remove this blank line from all files, or atleast from all files in a folder.

Please Note I donot want any other blank lines to be removed from file except the last.

Please help!

Last edited by ruchimca; 04-08-2009 at 07:40 PM.. Reason: incomplete que
# 2  
Old 04-08-2009
Code:
for i in *.ext; do lines=`wc --chars $i | cut -d\  -f 1`; let lines-=1; dd if=$i of=$i.new bs=1 count=$lines ; mv $i.new $i; done

You could try this, even though I reckon it's a rather trivial and bumpy approach. The idea behind it is to copy the file but omit the last character and thus remove the blank line.

More elegant/performant solutions might be achieved with awk, which is a science of its own Smilie
# 3  
Old 04-08-2009
I would ask how your comparing the files? If you use 'diff' then add the '-B' option to ignore blank lines and the '-q' option to just report if the files are different. With that approach the blank lines are no longer an issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

Bash - sed - Remove first word from line which can begin eventually with blank

hello. How to remove first word from line. The line may or may not start with blank. NEW_PARAM1=$(magic-command " -t --protocol=TCP -P 12345-u root -h localhost ") NEW_PARAM2=$(magic-command "-t --protocol=TCP -P 12345 -u root -h localhost ") I want NEW_PARAM1 equal to NEW_PARAM2 equal ... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

4. Shell Programming and Scripting

How to remove all blank spaces in a file

I have a file which contains data such as that shown below. How do i remove all the blcnak spaces, before, during and at the end of each line in one command? 300015, 58.0823212, 230.424728 300016, 58.2276459, 229.141602 300017, 58.7590027, 226.960846 ... (9 Replies)
Discussion started by: carlr
9 Replies

5. 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

6. Shell Programming and Scripting

Delete lines containing and remove the blank line at the same time

Is there a way to delete a line containing something and the blank line at the same time? If you do this it leaves a blank line behind. sed '/yum/d' .bash_historyI know this works but I would think there would be a way to do it with one command sed '/yum/d' .bash_history | sed '/^$/d'In... (2 Replies)
Discussion started by: cokedude
2 Replies

7. Shell Programming and Scripting

how to remove blank spaces in file

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to remove blank spaces..then how to read this file.. (1 Reply)
Discussion started by: shubhig15
1 Replies

8. Shell Programming and Scripting

Can't remove blank lines from a file

Hi Guys, I have been trying to remove blank lines from a file with no success. I tried using all the following options on the file: tr -s '\n' < abc.txt grep -v "^$" abc.txt sed '/^$/d' abc.txt sed '/./!d' abc.txt awk '/./' abc.txt The file is a text file. (11 Replies)
Discussion started by: npatwardhan
11 Replies

9. UNIX for Dummies Questions & Answers

Question: Help need to remove blank line & sed: Couldn't re-allocate memory error.

I've shell script where i used the below command to take the line which contains patterns. sed -n "/$year 05:/,/$year 17:/p" trace.log | grep -f patterns.txt > output.log This was working fine for long time, but now a days this script is not working with and throwing error like sed:... (8 Replies)
Discussion started by: senthil.ak
8 Replies

10. Shell Programming and Scripting

Remove blank line - SED

HI, I have this list of apps like so: DivX Products.app DivX Support.app Uninstall DivX for Mac.app Build Applet.app SpringBoard.app Interface.app MobileAddressBook.app MobileSafari.app MobileSlideShow.app Preferences.app Install Flash Player 8 OSX.app Yap.app check_afp.app ... (10 Replies)
Discussion started by: pcwiz
10 Replies
Login or Register to Ask a Question