How to strip off extra blank lines from the end of a text file?

I have a text file that has been formatted to be exactly 60 lines long even if the text is less. It has extra blank lines padding the end. What UNIX script or command can I use to get rid of the extra blank lines at the end of the file?
DIGITAL UNIX
rbendAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AstromanCommented:
Do you only want to get rid of the blank lines at the end?

sed -e '/^$/d' <file>

Will get rid of all blank lines, e.g.

"
hello


my friend



"
becomes
"
hello
myfriend
"
0
ozoCommented:
perl -0777 -i -pe 's/\s+$/\n/' file
0
rbendAuthor Commented:
ozo: there is no perl command on my system.
Astroman: only want the blank lines at the end to go. How do I write that?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

rbendAuthor Commented:
Astroman:
I tried your script, and I can see the results as it is executing, but when I look at the file afterword, there has been no change to it. ??
0
rbendAuthor Commented:
Astroman: Sorry... I directed it to "file2" and I got the end result. Still need to know how to do just the ones at the end.
0
AstromanCommented:
Sorry, I never thought of mentioning that sed is a filter. what you should nevre do is:

sed <cmd> FILE1 > FILE1

As it will just leave you with an empty file.

Not sure immediatly how to do only the lines at the end using standard unix commands, but I'll work on it.

Perl is free software though, if you want to install it, and it's very useful for all sorts of things.

look at www.perl.org

I'll be back! (but not for a few hours)
0
ozoCommented:
does `tail` on your system have a -r option?
0
kiffneyCommented:
if you have bash, the following might work (call it crclean):

#!/bin/bash
CRFLAG=""
fixup()
{
while read ;  do  
        if [ "$CRFLAG" != "" ] ; then
                echo "$REPLY" >> crtac
        elif [ -l "{$REPLY}" -gt 2 ] && [ "$CRFLAG" = "" ] ; then
                CRFLAG="yes"
                echo "$REPLY" > crtac
        fi
 done
}
tac $1 | fixup
tac crtac > crfixed
rm crtac
# invoke this with 'crclean myfile'
# your cleaned-up file is now called 'crfixed'
# you may have to edit this to change paths for the temporary file and the output file
0
AstromanCommented:
sorry, im off travelling for amonth now, but it looks like you're in capable hands...

Leon (aka Astroman)
0
rbendAuthor Commented:
OZO: yes, tail has a -r option.//
0
rbendAuthor Commented:
This question has a deletion request Pending
0
kiffneyCommented:
This question no longer is pending deletion
0
kiffneyCommented:
Why are you deleting my very nice answer, rbend?  It would be one thing if you responded and said,'that script doesn't work' or whatever.  This way it looks like you have got your answer but don't want to give up any points. That's a fine way to act.  Leave it up here and maybe it will help someone else out some day.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kar-hungCommented:
Please try to run the following script by supplying the input filename and the output filename as the first and the second parameters.

#!/bin/sh
if [ $# -lt 2 ]; then
  clear
  printf "\n\n You have to submit input filename as the first argument...\n"
  printf " You have to submit output filename as the second argument...\n\n"
  exit 0
else
  tail -r $1 > tmp1
  `awk '{if (length == 0) n++; else m=n; print m}' tmp1 > tmp2`
  `awk '{if (length != 0) print $1}' tmp2 > tmp3`
  sort -u -o tmp5 tmp3
  head -2 tmp5 > tmp6
  tail -1 tmp6 > tmp4
  firstln=`awk '{print $1}' tmp4`
  tail +$firstln tmp1 > tmp7
  tail -r tmp7 > $2 && rm tmp*
  exit 0
fi


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.