Blank Lines - End of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Blank Lines - End of file
# 1  
Old 07-11-2003
Blank Lines - End of file

Hi all
I need to strip blank lines from the end of a file. I have searched and found topics on how to strip lines from the entirety of a file - however I need to limit this to only the last 3-4 lines.

Any ideas?

Thanks
# 2  
Old 07-11-2003
Here's one solution using awk, from www.experts-exchange.com:
Code:
#!/usr/bin/awk -f

BEGIN { nb = 0; }

{
 if ( $0=="" )
 {
    nb++;
 }
 else
 {
   if( nb > 0 )
   {
     for(i = 0; i < nb; i++) print "";
     nb = 0;
   }
   print $0;
 }
}

Name the script something like yourScript and use it like this:

yourScript < yourFile > TMP_00
mv TMP_00 yourFile
# 3  
Old 07-11-2003
MySQL

Nice one, thanks. Smilie
# 4  
Old 07-11-2003
using head, tail and sed

Heres a little gizmo from my collection, its crude (no param checking) but it works




#!/bin/bash
#linechomp , removes trailing lines from end of file
numlines=`eval "cat $2 | wc -l"`
let splithere="$numlines-$1"
cat $2 | head -$splithere > $3
cat $2 | tail -$1 | sed -e '/^$/d' >> $3

usage: linechomp n foo bar

n is 3 or 4 in your case

Oomberas code might be better though as this doesn't guarantee you wont get

Line of some stuff, blah blah blah
<blank line>
Line of some more stuff
<blank line>

as your last 4 lines
# 5  
Old 07-15-2003
Here's the same solution as oombera's though written in a more awk-like and less C-like fashion. Note that I'm also changing the problem somewhat: This will trim trailing lines consisting only of blanks and tabs (i.e., apparently empty) as well as truly empty lines:
Code:
#!/usr/bin/awk -f
NF == 0 { nb++ ; next }
nb      { for (i = 1; i <= NF; i++) print "" }
        { nb = 0; print }

Invoke it the same way as oombera's code. If you want only the truly empty lines to be whacked off the end, just change the first test to length == 0.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting a blank line at the end of a .txt file?

Hi there, I am having this problem: a) I am uploading a txt file from windows (notepad) with some Gaussian 09 command lines; b) Gaussian needs in certain command files, that those files have a blank line at the end of the file! c) I open the command file with vi and no blank line at the of... (2 Replies)
Discussion started by: luismga
2 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

4. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

5. Shell Programming and Scripting

default blank line in end of file

Dear Experts, I have spent my full day looking for the solution..:wall:but could no resolved. Here it goes: I have a txt file say data.txt. The content is shown below: ************** datav1 datav2 datav3 *********** I have shown the blank line knowingly. If I open this txt file using... (12 Replies)
Discussion started by: nrjrasaxena
12 Replies

6. UNIX for Dummies Questions & Answers

Add blank line to end of file

Alright, so I was looking around a bit on the forum before posting and still don't really understand so I figured I'd post my own question. I am appending two files using cat file_1.txt >> file_2.txt The problem is that I need a blank line in between the two different text files and it does... (2 Replies)
Discussion started by: cgagnon
2 Replies

7. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

8. Shell Programming and Scripting

deleting blank lines ONLY at the end of the file

Hi Guys, I have a quetion which was already discussed in the forum, but for some reason all approches suggested fail for me. I have a file which have blank lines at the body of the text as well as at the end. I need to delete ONLY blank lines at the end. Unfortunatly the approach below does not... (5 Replies)
Discussion started by: aoussenko
5 Replies

9. Shell Programming and Scripting

how to add blank spaces at the end of every record in a file.

hi, Does anyone has any idea in adding few blank spaces at the end of every record in a file. Eg: file.txt Baby Boy Kim 1234 Baby Boy Vik 1334 Desired output:- output.txt Baby Boy Kim 1234 Baby Boy Vik 1334 I want to add 10 blank spaces at the end every record in file.txt (3 Replies)
Discussion started by: techmoris
3 Replies

10. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies
Login or Register to Ask a Question