Solved

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

Posted on 2000-04-28
14
247 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:rbend
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 1

Expert Comment

by:Astroman
ID: 2760340
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
 
LVL 84

Expert Comment

by:ozo
ID: 2760407
perl -0777 -i -pe 's/\s+$/\n/' file
0
 

Author Comment

by:rbend
ID: 2760427
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
 

Author Comment

by:rbend
ID: 2760462
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
 

Author Comment

by:rbend
ID: 2760471
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
 
LVL 1

Expert Comment

by:Astroman
ID: 2760489
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
 
LVL 84

Expert Comment

by:ozo
ID: 2760516
does `tail` on your system have a -r option?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 4

Expert Comment

by:kiffney
ID: 2762157
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
 
LVL 1

Expert Comment

by:Astroman
ID: 2762619
sorry, im off travelling for amonth now, but it looks like you're in capable hands...

Leon (aka Astroman)
0
 

Author Comment

by:rbend
ID: 2765718
OZO: yes, tail has a -r option.//
0
 

Author Comment

by:rbend
ID: 2766364
This question has a deletion request Pending
0
 
LVL 4

Expert Comment

by:kiffney
ID: 2766405
This question no longer is pending deletion
0
 
LVL 4

Accepted Solution

by:
kiffney earned 50 total points
ID: 2766406
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
 

Expert Comment

by:kar-hung
ID: 2766497
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now