Solved

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

Posted on 2000-04-28
14
248 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
error while running the scirpt 24 125
removing nim resources 5 48
how to check for shares on aix 1 66
unix in java example 9 53
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

896 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

14 Experts available now in Live!

Get 1:1 Help Now