stripping trailing blank lines

stripping trailing blank lines

Post by Cliff Tu » Fri, 17 Jul 1992 15:53:21



I'm looking for a quick and dirty way to strip trailing blank lines
from a file.  I know I can use a script based on "tail -1" and a
temporary file, but is there an easier way to do it with sed, awk,
perl, etc.?

------ Sample input ------
this line has text
so does this one
the next line is blank

the previous line is blank
the next two lines are blank

------ Sample output -------
this line has text
so does this one
the next line is blank

the previous line is blank
the next two lines are blank
----------------------------

--
Cliff Tuel                   __o o__
HP/Apollo System Admin       \( v )/       YELLO & ART OF NOISE mailing lists:


 
 
 

stripping trailing blank lines

Post by Tom Christianse » Fri, 17 Jul 1992 22:33:48



:I'm looking for a quick and dirty way to strip trailing blank lines
:from a file.  I know I can use a script based on "tail -1" and a
:temporary file, but is there an easier way to do it with sed, awk,
:perl, etc.?
:
:------ Sample input ------
:this line has text
:so does this one
:the next line is blank
:
:the previous line is blank
:the next two lines are blank
:
:
:------ Sample output -------
:this line has text
:so does this one
:the next line is blank
:
:the previous line is blank
:the next two lines are blank
:----------------------------

I'm sure someone else will provide a sed solution and maybe even and awk
one, and if Larry Wall isn't too busy, he'll give us one in nroff.  

But me, I'm lazy, so I'll do it in perl.

    perl -0777 -e 's/\n+$/\n/'

There, wasn't that fast to write and easy to read?

The -0777 flag is the principal magic here: it says to treat
the whole file as one string.  Given that, the substitution
just falls out.

Several other substitutes would have been equally valid:

    s/\n\n*$/\n/

    s/\n{2,}$/\n/

--tom
--

If mathematically you end up with the wrong answer, try multiplying by
the page number.

 
 
 

stripping trailing blank lines

Post by Tom Christianse » Sat, 18 Jul 1992 04:27:58


:    perl -0777 -e 's/\n+$/\n/'

But I meant

    perl -0777 -pe 's/\n+$/\n/'

because while 'sed -n' is like 'perl -n', to get 'sed' w/o the -n, you
need 'perl -p' in order to make it iterate through the file and print the
pattern space after munging.

--tom
--

I've been told to keep my remarks relatively brief. I understand Quayle-hunting
season begins at noon.  --Dan Quayle to a crowd in Eau Claire, Wisc.

 
 
 

stripping trailing blank lines

Post by David W. Tamk » Sat, 18 Jul 1992 11:18:09




| :I'm looking for a quick and dirty way to strip trailing blank lines
| :from a file.  I know I can use a script based on "tail -1" and a
| :temporary file, but is there an easier way to do it with sed, awk,
| :perl, etc.?

| I'm sure someone else will provide a sed solution and maybe even and awk
| one, and if Larry Wall isn't too busy, he'll give us one in nroff.  
|
| But me, I'm lazy, so I'll do it in perl.

Tom was right that someone would provide a solution in sed.  Since perl
slurps in the whole file at once, I'm sure Tom's solution will run faster
despite the longer time to fork perl unless the file is VERY short, but
sed is about the limit of complexity for my talents.

sed ':x
/[!-~]/b
N
bx'

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

stripping trailing blank lines

Post by Larry Wa » Sun, 19 Jul 1992 05:29:02



: :I'm looking for a quick and dirty way to strip trailing blank lines
: :from a file.  I know I can use a script based on "tail -1" and a
: :temporary file, but is there an easier way to do it with sed, awk,
: :perl, etc.?
: :
: :------ Sample input ------
: :this line has text
: :so does this one
: :the next line is blank
: :
: :the previous line is blank
: :the next two lines are blank
: :
: :
: :------ Sample output -------
: :this line has text
: :so does this one
: :the next line is blank
: :
: :the previous line is blank
: :the next two lines are blank
: :----------------------------
:
: I'm sure someone else will provide a sed solution and maybe even and awk
: one, and if Larry Wall isn't too busy, he'll give us one in nroff.  

Whaddya mean, "too busy"?  It's not as if it takes all day.

#!/bin/sh
(
    cat <<-'END'
        .de TR
        'di
        .pl 1000v
        .aa
        .ST
        ..
        .de ST
        .pl 1n
        'di aa
        .nr .i 0
        .it 1 TR
        ..
        .nf
        .ST
        END

) | nroff

Seems perfectly straightforward to me...  :-)

Larry

 
 
 

stripping trailing blank lines

Post by David W. Tamk » Tue, 21 Jul 1992 13:54:48



Cliff Tuel's need to strip trailing blank lines from the foot of a file:

sed ':x
/[!-~]/b
N
bx'

Tom Christiansen swore to me that it didn't work and advised me to post a
retraction.  I swore back that it worked just fine and he should check his
transcription and try it again.  It still didn't work for him.

In the sed versions I've used, executing an N on the last line causes an exit
without printing the pattern space, even if -n is not on.  I had the idea [uh
oh] that maybe the sed at Convex did the more logical thing and treated N on
the last line the same as n or q: you're exiting the script, so if -n isn't
on, print the pattern space.  I asked him to try this to strip trailing blank
lines:

sed ':x
/[!-~]/b
$d
N
bx'

and indeed it worked for him.  Ergo, for those of you in whose sed versions
$N means $q, the second script will strip trailing blank lines and the first
will do nothing but cat the input.  For those of us in whose sed versions
$N means $d, both scripts work.

If you're one of the contingent who hate sed branches and love using its hold
space, here's another sed script for the same problem:

sed -n 'H
/[!-~]/!d
g
s/.//p
s/.*//
h'

David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297

 
 
 

stripping trailing blank lines

Post by lw.. » Sun, 19 Jul 1992 09:29:00



: :I'm looking for a quick and dirty way to strip trailing blank lines
: :from a file.  I know I can use a script based on "tail -1" and a
: :temporary file, but is there an easier way to do it with sed, awk,
: :perl, etc.?
: :
: :------ Sample input ------
: :this line has text
: :so does this one
: :the next line is blank
: :
: :the previous line is blank
: :the next two lines are blank
: :
: :
: :------ Sample output -------
: :this line has text
: :so does this one
: :the next line is blank
: :
: :the previous line is blank
: :the next two lines are blank
: :----------------------------
:
: I'm sure someone else will provide a sed solution and maybe even and awk
: one, and if Larry Wall isn't too busy, he'll give us one in nroff.  

Whaddya mean, "too busy"?  It's not as if it takes all day.

#!/bin/sh
(
    cat <<-'END'
        .de TR
        'di
        .pl 1000v
        .aa
        .ST
        ..
        .de ST
        .pl 1n
        'di aa
        .nr .i 0
        .it 1 TR
        ..
        .nf
        .ST
        END

) | nroff

Seems perfectly straightforward to me...  :-)

Larry

 
 
 

1. Stripping trailing blanks in a string.

This works for me:

sed -e 's/ *$//'

Cheers,
Zoran
--
char *Disclaimer = "My opinions only. Use at your own risk.";
char *Murphy =  "NON-RECIPROCAL LAWS OF EXPECTATIONS:"
                "  Negative expectations yield negative results."
                "  Positive expectations yield negative results.";

2. Linux Peer-to-Peer networking problems

3. Stripping blank lines from top and bottom of file.

4. Autoplay mp3 in Konq

5. Strip blank lines from a .txt file

6. miniroot in Solaris 2

7. vi: convert double blank lines to single blank lines???

8. Serial Line is Looped Back error

9. multiple blank lines -> one blank line

10. vi: convert double blank lines to single blank lines???

11. HELP: How do I read a file line-by-line without getting spaces stripped?

12. XFree86 4.01: blank lines in terminal and blank terminal???

13. Alphanumeric sorting with trailing blanks