[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: regex question



On Sat, Apr 12, 2008 at 06:26:26PM -0700, Daniel Burrows wrote:
> On Sun, Apr 13, 2008 at 09:37:02AM +1000, Alex Samad <alex@samad.com.au> was heard to say:
> > this started out as a grep quetion
> > 
> > trying to look at a file with out the comments in it
> > 
> > i tried grep -v '^\s*;'  ; is the comment delimiter.
> > 
> > but this left me with lots of blank lines.
> 
>   Have you tried this?
> 
>     egrep -v '\s*(;|$)'
Yes I did (thanks twice), but my question which arose from this was why
this did not work 

perl -nle 'next if ( /^\s*;/);  print' sip.conf

why do I need the length statement 

perl -nle 'next if ( /^\s*;/);  print  if length $_ > 0' sip.conf

with a sample file of
line 1
;line 2
    ;line3
line 4


with the egrep and one -v '^\s*;' why do I end up with 
line 1


line 4

Why do I get the blank lines ?  shouldn't those blank lines not be
printed.  If I turned it around to egrep '^\s*;' I would get
;line 2
    ;line3

and no blank lines.


The same with the perl statement perl -nle 'next if ( /^\s*;/);  print'

my understanding is this expands to 

while(<>){
	chomp;
	next if (^\s*;);
	print;
}

so when it comes time to process line2, it should match the if and goto
next, which should then read the next line, which matches and then the
next line at which point it should print out line 4.

But it doesn't it prints out a ^$ for line 3 & 4, I am curious to know
why.

Alex


> 
>   Seems a lot easier than writing hairy Perl code (but I repeat myself).
> 
>   Daniel
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org 
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> 
> 

-- 
Not affiliated with the American Red Cross.

Attachment: signature.asc
Description: Digital signature


Reply to: