Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to exclude the lines that start with 0?

Reply
Thread Tools

How to exclude the lines that start with 0?

 
 
Rider
Guest
Posts: n/a
 
      09-26-2010
Hi experts,

It has been a long time I fiddled with perl scripts.

I want to exclude all the lines that start with 0 and print only the
lines starting with non-zeroes.

Here is the script and I am not sure what is wrong here. Can some one
help me in correcting the syntax error here? Or even a short-cut like
one liner?

+++++++++++++++++++++
while(<DATA>)
{
if (^$_ == 0)
{
next;
print;
}
}

__DATA__
0 x.txt
0 y.txt
0 z.txt
101 z.txt
203 a:txt
303 l.txt
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-26-2010
Rider <> wrote:
>I want to exclude all the lines that start with 0 and print only the
>lines starting with non-zeroes.
>
>Here is the script and I am not sure what is wrong here. Can some one
>help me in correcting the syntax error here? Or even a short-cut like
>one liner?
>
>+++++++++++++++++++++
>while(<DATA>)
> {
> if (^$_ == 0)


What is
^$_
supposed to do? Even if you meant to write a RE, it still doesn't make
much sense to numerically compare it to 0.

> {
> next;
> print;
> }
>}


while (<>){
print unless /^0/;
}

while (<>){
print unless substr($_, 0, 1) eq '0';
}

I'm sure there are many more possible ways.

jue
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      09-26-2010
On 2010-09-26 03:50, Rider wrote:

> I want to exclude all the lines that start with 0 and print only the
> lines starting with non-zeroes.


perl -ne '/^0/ or print' file

perl -ne '/^0/||print' file

perl -pe 's/^0.*//s' file

--
Ruud
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM
Regular expression to exclude lines? Shannon Jacobs Javascript 26 12-05-2003 10:05 PM



Advertisments