![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
||||
|
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;
}
}
yourScript < yourFile > TMP_00 mv TMP_00 yourFile |
|
|||
|
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 |