Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2000-04-28
14
Medium Priority
?
257 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 85

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 85

Expert Comment

by:ozo
ID: 2760516
does `tail` on your system have a -r option?
0
 
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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

782 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