Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Even and uneven numbers as RegEx

by New Novice (Sexton)
on Sep 27, 2004 at 11:24 UTC ( [id://394120]=perlquestion: print w/replies, xml ) Need Help??

New Novice has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perlmonks,

I have a file (using new line as a delimiter) that is at the moment storing each kind of information twice. Naturally, I would like to get rid of all duplicates. They come in bundles, so I was thinking of just reading out every second line using a loop and storing this in a new file.

My questions:

1. Is there a regular expression for "even number"/"uneven number"?

2. What is the code for "end of file"?

3. Is there a module that could do it (although I prefer to keep things simple)?

Thanks!

Replies are listed 'Best First'.
Re: Even and uneven numbers as RegEx
by broquaint (Abbot) on Sep 27, 2004 at 11:30 UTC
    A one-line solution would be
    perl -i.bak -ne 'print if $. & 1' filename
    That will remove all even numbered lines in the file.
    HTH

    _________
    broquaint

Re: Even and uneven numbers as RegEx
by grinder (Bishop) on Sep 27, 2004 at 12:26 UTC
    1. Yes, $num =~ /[02468]$/ (...with the proviso that some may consider that 0 (zero) is neither odd nor even).
    2. Yes, the function eof tells you if you are at the end of a file. In practise, you never need to use it, you simply keep on reading until you fall off the end.
    3. No, but if you are running on a Unix variant you should have the uniq command at your disposal, and it will probably run much faster than the Perl one-liners mentioned elsewhere in this thread. Using uniq has another advantage: if on occasion a line is not followed by a duplicate, it will emit both lines. The modulo-2 approach, on the other hand, will get out of sync and begin throwing away good data.

    - another intruder with the mooring of the heat of the Perl

Re: Even and uneven numbers as RegEx
by zejames (Hermit) on Sep 27, 2004 at 11:31 UTC
    I would do this :

    my $i; while (<DATA) { print if ($i++ % 2); } __DATA__ 435 435 f,sdlf,dsl f,sdlf,dsl 64{# 64{# Blah Blah
    or, for a one liner :
    perl -i -ne 'print if ($i++ % 2)' filename

    Here $i % 2 is a mean to know if $i is even or not.
    HTH

    --
    zejames

      Why bother with your own $i variable when Perl gives you $. for free?

      perl -i -ne 'print if $. % 2' filename

      (which is, of course, pretty much what broquaint had above)

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://394120]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found