LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] silly shell thing

[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
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell