The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Strip one line from 2 blank lines in a file tipsy Shell Programming and Scripting 6 06-23-2008 05:14 AM
remove blank lines in *.srt file :) hungbp UNIX for Dummies Questions & Answers 10 02-16-2008 04:40 AM
delete blank lines from a file sachin.gangadha UNIX for Dummies Questions & Answers 2 12-04-2007 03:13 PM
Delete blank lines at the end of file TL56 Shell Programming and Scripting 3 10-25-2007 12:44 PM
regex to delete multiple blank lines in a file? fedora Shell Programming and Scripting 6 10-11-2007 01:36 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2003
Registered User
 

Join Date: Sep 2002
Location: UK
Posts: 65
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
__________________
Free Palestine
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-11-2003
oombera's Avatar
Have a day :|
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
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
Reply With Quote
  #3 (permalink)  
Old 07-11-2003
Registered User
 

Join Date: Sep 2002
Location: UK
Posts: 65
Thumbs up

Nice one, thanks.
__________________
Free Palestine
Reply With Quote
  #4 (permalink)  
Old 07-11-2003
Registered User
 

Join Date: Jul 2003
Location: UK
Posts: 27
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
Reply With Quote
  #5 (permalink)  
Old 07-15-2003
criglerj's Avatar
Registered User
 

Join Date: May 2002
Location: Atlanta
Posts: 129
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.
Reply With Quote
Google UNIX.COM
Reply

Tags
bash, bash eval, eval

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0