[ILUG] silly shell thing

Niall O Broin niall at magicgoeshere.com
Wed May 24 13:14:06 IST 2000


On Wed, May 24, 2000 at 12:34:28PM +0100, John P. Looney (Kate) wrote:

> On Wed, May 24, 2000 at 01:08:24PM +0100, Niall O Broin mentioned:
> > >  Anyone got a good way of getting rid of them ? I tryed:
> > > 
> > >     cat file | tr -s \\n
> > > 
> > >  and
> > > 
> > >     cat file | sed -e 's/\n\n/\n'
> > 
> > I've just realised that my earlier answer could have been shorter and
> > perlier - I'll be drummed out of Perl-mongers :-). This works nicely
> > 
> > 
> > perl -lne 'print if length' screwed_up_file
> 
>  Didn't seem to do anything.
> 
>  What does it do ?

That's strange - it works fine here, but I'm sure you're familiar with that
from your tech support days. It runs perl (surprise) with the single line
script (the -e switch)


print if length

which iterates over its arguments (the -n switch) and reads them line by
line. It takes the line ending character off automagically and puts it back
on with a print statement. So each input line which is empty doesn't get
printed because length is false. It works fine here - e.g.

niall at bagend:~ > cat double
a
 
b
 
c
 
d
 
niall at bagend:~ > perl -lne 'print if length' double
a
b
c
d

but I've just realised that it has a flaw (typical one liner thing) - if you
have any blank lines in your input file which should be there it'll dump
them too. But not to worry - try this instead (example included)

niall at bagend:~ > cat double_with_blank
a
 
 
 
b
 
c
 
d
 
niall at bagend:~ > perl -lne 'print if ($skip = !$skip)' double_with_blank
a
 
b
c
d


which relies on the autovivification of undeclared variables (for the
programming purists - you can turn this off , and are recommended to for any
but trivial programs) with a value of false.


But I'm still a little puzzled - what did the original do with your input
file ?



Regards,


Niall





More information about the ILUG mailing list