Removing multiple blank lines from text files

Removing multiple blank lines from text files

Post by Matt Bear » Thu, 03 Jul 1997 04:00:00



Hi

I want to replace multiple consecutive empty lines with one empty line,
so that there is never more than one empty line between lines containing
characters.

On DEC Alphas I have previously used the command "cat -r", which does
the job nicely.  On a Sun, running Solaris, however, the "-r" option
does not exist for "cat".  

Therefore can anyone tell me how to do it using "sed", or some other
command ?  (Please note, Perl solutions are not an option for me).

Thanks,
        Matt.

------------------------------------------------------------------------

School of Mathematics      |  Tel: +44 (0)1603 592990
University of East Anglia  |  Fax: +44 (0)1603 259515
Norwich  NR4 7TJ  England  |  http://www.mth.uea.ac.uk/people/mib.html
------------------------------------------------------------------------

 
 
 

Removing multiple blank lines from text files

Post by David Boyc » Thu, 03 Jul 1997 04:00:00



> Hi

> I want to replace multiple consecutive empty lines with one empty line,
> so that there is never more than one empty line between lines containing
> characters.

> On DEC Alphas I have previously used the command "cat -r", which does
> the job nicely.  On a Sun, running Solaris, however, the "-r" option
> does not exist for "cat".

> Therefore can anyone tell me how to do it using "sed", or some other
> command ?  (Please note, Perl solutions are not an option for me).

I believe "more -s" is the POSIX (portable) way of doing this.

David

 
 
 

Removing multiple blank lines from text files

Post by Stewart Ravenhal » Thu, 03 Jul 1997 04:00:00


#!/usr/bin/sed -nf
# Stewart Ravenhall
/^ *$/!p
/^ *$/{
    p
    :ubble
    n
    /^ *$/bubble
    p

Quote:}

Hope this helps.

Thanks to all the smashing guys and gals at comp.unix.shell who've helped
me with my sed problems in the past so that I've learnt enough to be able
to write this sort of thing!
:-)

Best wishes,
Stew.
--
Stewart Ravenhall,
Milton Keynes,
Buckinghamshire,
England.




Quote:> I want to replace multiple consecutive empty lines with one empty line,
> so that there is never more than one empty line between lines containing
> characters.

 
 
 

Removing multiple blank lines from text files

Post by Al A » Wed, 09 Jul 1997 04:00:00


for a shorter sed script

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    for a 1stop sed needs, join seders, the informal sed mailing list.      
    sed is literate.  sed knows 4 R's: read, 'rite, 'rithmetic, recur.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                        sed informal mailing list

        Of half a dozen sed studs known to sedkind, seders has 2   :
                Greg Ubben       +      Carlos J. G. Duarte.
                               -----

                   sed web pages (in 4 countries)

http://www.wollery.demon.co.uk   seders grab bag (seders official web page
                                                              seder  Casper
http://www.math.fu-berlin.de/~guckes/sed/          seder, Herr Guckes
http://www.math.fu-berlin.de/~leitner/sed/          seder, Herr von Leitner
http://www.dbnet.ece.ntua.gr/~george/#seders  seder, engineer, Dr2b Yiorgos
ftp://olivia.inesc.pt/pub/users/cdua/scripts/sed         the great sed stud
ftp://olivia.inesc.pt/pub/users/cdua/scripts/sh   seder Carlos J. G. Duarte
                                ----

             my favourite DOS/UNIX sed15 (with C source)
(easily compilable for UNIX.  has friendly extenstions/relaxations like hex)
 ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15.zip
 ftp://uiarchive.uiuc.edu/pub/systems/pc/simtelnet/msdos/txtutl/sed15x...

Directory: /pub/systems/pc/simtelnet/msdos/txtutl/
Filename    Type Length  Date    Description
===============================================================================
sed15.zip    B    62082  910930  Unix-compatible streaming editor v1.5 TC src
sed15x.zip   B    20300  910930  Unix-compatible streaming editor v1.5 EXE/docs

              sed15.zip has C source, compilable for UNIX.

             Also, check simtel for u-sedit. has nice docs.

                                ----

                O'Reilly sed books http://www.ora.com
1- sed and awk 2nd edition         by   Dale Dougherty  +  Arnold Robbins
        (mostly for awk)
2- mastering regular expressions   by          Jeffrey E. F. Friedl
        (mostly for perl, emacs, awk.  but touches on sed)

                                ----
                   newsgroup alt.comp.editors.batch      
      though seders news group, alt.comp.editors.batch deals with
batch, AI, data warehousing, OLAP, computer security, filters, linguistics,
      awk, perl, unix, computer seminars, CFP, etc (even sed)

           started by a seder, PhD2b, engineer, the hawk
                         Yiorgos Adamopoulos.    

          most newsfeeds do not carry alt.comp.editors.batch
          but everyone can get it via  dejanews (and others)
  you can even follow up, reply to the poster, etc.  dejanews is great.
             (then he sed: not by sed alone ... lives man)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

: #!/usr/bin/sed -nf
: # Stewart Ravenhall
: /^ *$/!p
: /^ *$/{
:     p
:     :ubble
:     n
:     /^ *$/bubble
:     p
: }

: Hope this helps.

: Thanks to all the smashing guys and gals at comp.unix.shell who've helped
: me with my sed problems in the past so that I've learnt enough to be able
: to write this sort of thing!
: :-)

: Best wishes,
: Stew.
: --
: Stewart Ravenhall,
: Milton Keynes,
: Buckinghamshire,
: England.




: > I want to replace multiple consecutive empty lines with one empty line,
: > so that there is never more than one empty line between lines containing
: > characters.

--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, seders moderator                                      sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

 
 
 

1. Removing groups of blank lines form a text file

Hi.  I have a text file which is in 68 line sections (pages).  I think
what I'd like to do is go to the 68th line and delete all consecutive
previous lines which are blank and then repeat the process for the
remainding 68 line sections.  The number of blank lines can vary.
All lines are 133 characters (don't think this matters).  The file is
quite large, ie,. > 100 mb.  I have to do this each month for a while
so the sixe of the file will vary.  I'm concerned about resources --
buffer space, etc., and have to be careful how I do this.  This is on
an AIX box and I'm not sure if ksh is suitable.  Any comments
appreciated.

cheers.

2. Blinking lines in /etc/issue

3. remove all blank lines in text file

4. X-Windows will not recognize my MS PS/2-mouse - can't succeed with xf86config.

5. trim multiple blanks in text file to just one blank

6. xdm

7. multiple blank lines -> one blank line

8. modem : USR 28.8 Fax Modem, script fails

9. Removing lines from text file based on line number

10. (sh) Using sed to remove multiple blank lines

11. eliminate blank lines from text file

12. Inserting blank lines in a text file..

13. How to skip blank lines when reading a text file?