FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 04-12-2008, 11:37 PM
Alex Samad
 
Default regex question

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.

so i tried changing it to perl

perl -e 'while(<>){chomp ; next if ( /^s*;/); print "[$_]
"}'
sip.conf

which still prints out the blank lines

this works
perl -e 'while(<>){chomp ; next if ( /^s*;/); print "[$_]
" if length
$_ > 0}' sip.conf

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

why doesn't the next force a read of a new line and thus a new $_, why
do I have to check for length of $_

Alex

--
"I do not believe, however, that the government should impose on power plants mandatory emissions reductions for carbon dioxide, which is not a 'pollutant' under the Clean Air Act."

- George W. Bush
03/13/2003
in a letter to Sen. Chuck Hagel (R-NE)
 
Old 04-13-2008, 12:08 AM
"Douglas A. Tutty"
 
Default regex question

On Sun, Apr 13, 2008 at 09:37:02AM +1000, Alex Samad wrote:

> this works
> perl -e 'while(<>){chomp ; next if ( /^s*;/); print "[$_]
" if length
> $_ > 0}' sip.conf
>
> or this
> perl -nle 'next if ( /^s*;/); print if length $_ > 0' sip.conf
>
> why doesn't the next force a read of a new line and thus a new $_, why
> do I have to check for length of $_

I don't know. This looks like a prime example of why I hate regex (or
it hates me). I'd do it the long way in python with readlines, discard
empty lines and slice off comments to create a sequence of cooked lines.

Don't ask me how to do that in perl.

Don't get me wrong, the ability to do this in one-liners is certainly
powerful. Sed and grep are also likely quite CPU efficient, but I
wouldn't call any of these lines readable.

Doug.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-13-2008, 01:22 AM
"Owen Townend"
 
Default regex question

On 13/04/2008, Alex Samad <alex@samad.com.au> wrote:
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.


Hey,
**Try this:
`sed -e 's:;.*::' -e ':^$:d' abc.ccp`
**It removes everything from ';' to the end of the line and removes empty lines.

**There's a great sed tutorial site here: http://www.grymoire.com/Unix/Sed.html


cheers,
Owen.
 
Old 04-13-2008, 01:26 AM
Daniel Burrows
 
Default regex question

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*(;|$)'

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
 
Old 04-13-2008, 06:37 AM
 
Default regex question

Alex Samad wrote:
> 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.

Try this:

sed 's/[[:space:]]*;.*//' file | less -s

The 'less -s' squeezes consecutive blank lines. See also 'more -s'
and 'cat --squeeze-blank' (but beware that 'cat -s' on SysV systems
means to be silent about missing files).

Bob
 
Old 04-13-2008, 11:54 AM
Alex Samad
 
Default regex question

On Sun, Apr 13, 2008 at 11:22:13AM +1000, Owen Townend wrote:
> On 13/04/2008, Alex Samad <alex@samad.com.au> wrote:
> >
> > 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.
> >
>
> Hey,
> Try this:
> `sed -e 's:;.*::' -e ':^$:d' abc.ccp`

yeah but it still leave the empty lines, you can join -e together with ;

sed -e 's:;.*::;:^$:d' <file>

> It removes everything from ';' to the end of the line and removes empty
> lines.
>
> There's a great sed tutorial site here:
> http://www.grymoire.com/Unix/Sed.html
>
> cheers,
> Owen.

--
"As a matter of fact, I know relations between our governments is good."

- George W. Bush
11/08/2005
Washington, DC
On U.S.-South Korean relations
 
Old 04-13-2008, 12:00 PM
Alex Samad
 
Default 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.
 
Old 04-13-2008, 02:21 PM
"Mumia W.."
 
Default regex question

On 04/13/2008 07:00 AM, Alex Samad wrote:

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

[...]


First, this might be easier to understand:

perl -nle 'print unless /^s*(#|$)/' sources.list
(Replace "#" with ";".)

Secondly, the "length" statement was needed for your earlier code
because /^s*;/ won't match blank lines.

Take this code:
perl -nle 'next if ( /^s*#/); print if length $_ > 0' sources.list

Simplify it a bit and use more regex's:
perl -nle 'next if (/^s*#/ or /^s*$/); print' sources.list

Integrate the regex'es into a single one:
perl -nle 'next if /^s*(#|$)/; print' sources.list

Eliminate "next":
perl -nle 'print unless /^s*(#|$)/' sources.list


HTH



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-13-2008, 03:00 PM
Daniel Burrows
 
Default regex question

On Sun, Apr 13, 2008 at 10:00:42PM +1000, Alex Samad <alex@samad.com.au> was heard to say:
> 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

As you probably guessed from my mail, I don't speak perl; I was just
trying to solve the problem you said you started with. :-) From your
comments above it sounded like you wanted to remove comments and *also*
blank lines.

> 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

I can't reproduce this. You're saying that "egrep -v '^s*;'" does
this? Because I just get

line 1
line 4

back, as I would expect.

Could there be some odd characters in the input file that are
confusing grep?

Daniel


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 04-13-2008, 08:48 PM
Alex Samad
 
Default regex question

On Sun, Apr 13, 2008 at 09:21:51AM -0500, Mumia W.. wrote:
> On 04/13/2008 07:00 AM, Alex Samad wrote:
>> 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
>>
>> [...]
>
> First, this might be easier to understand:
>
> perl -nle 'print unless /^s*(#|$)/' sources.list
> (Replace "#" with ";".)
>
> Secondly, the "length" statement was needed for your earlier code
> because /^s*;/ won't match blank lines.

I did not want to remove the blank lines of the file, I had presumed
their were not any blank lines in there. upon closer inspection there
are

So this now all makes sense


any now I feel a bit silly

Thanks everyone

>
> Take this code:
> perl -nle 'next if ( /^s*#/); print if length $_ > 0' sources.list
>
> Simplify it a bit and use more regex's:
> perl -nle 'next if (/^s*#/ or /^s*$/); print' sources.list
>
> Integrate the regex'es into a single one:
> perl -nle 'next if /^s*(#|$)/; print' sources.list
>
> Eliminate "next":
> perl -nle 'print unless /^s*(#|$)/' sources.list
>
>
> HTH
>
>
>
> --
> To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a
> subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
>
>

--
"I like to tell people when the final history is written on Iraq, it will look like just a comma because there is -- my point is, there's a strong will for democracy."

- George W. Bush
09/24/2006
in an interview on CNN
 

Thread Tools




All times are GMT. The time now is 09:58 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org