The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 08:14 AM
remove blank lines in *.srt file :) hungbp UNIX for Dummies Questions & Answers 10 02-16-2008 08:40 AM
delete blank lines from a file sachin.gangadha UNIX for Dummies Questions & Answers 2 12-04-2007 07:13 PM
Delete blank lines at the end of file TL56 Shell Programming and Scripting 3 10-25-2007 03:44 PM
regex to delete multiple blank lines in a file? fedora Shell Programming and Scripting 6 10-11-2007 04:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2003
saabir saabir is offline
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
  #2 (permalink)  
Old 07-11-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

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
  #3 (permalink)  
Old 07-11-2003
saabir saabir is offline
Registered User
  
 

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

Nice one, thanks.
  #4 (permalink)  
Old 07-11-2003
andyj andyj is offline
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
  #5 (permalink)  
Old 07-15-2003
criglerj's Avatar
criglerj criglerj is offline
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.
Closed Thread

Bookmarks

Tags
awk, awk trim, bash, bash eval, eval, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0