How to exclude the lines that start with 0?

R

Rider

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
 
J

Jürgen Exner

Rider said:
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
 
D

Dr.Ruud

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top