Using perl -e to remove times/dates from a file

Discussion in 'Perl Misc' started by Paul Tomlinson, Nov 28, 2003.

  1. All I have a file called "DirList.txt" which was generated by redirecting
    the results of a DOS dir to a file.

    What I want is to have perl remove the times (and preferably dates) from
    this file.

    I thought I could do it like such but it appears to do nothing, any
    pointers?

    c:\perl\bin\perl.exe -e tr/09:36//; "c:\DirList.txt"

    I know that this will only translate the time 09:36 to null but for now
    anything is a start.

    PT
     
    Paul Tomlinson, Nov 28, 2003
    #1
    1. Advertising

  2. Hi Paul,

    You need an introduction to regular expressions.

    The translation operator:

    tr/09:36//

    will remove all "0", "9", ":", "3" and "6" characters. It is really to be used for counting characters or tr/a-z/A-Z/ and suchlike.

    You need to be looking at the s/find/replace/ regex operator.

    perldoc perlrequick
    perldoc perlretut

    Cheers,
    Ben Liddicott

    "Paul Tomlinson" <> wrote in message news:bq77c5$1vbnc1$-berlin.de...

    > All I have a file called "DirList.txt" which was generated by redirecting
    > the results of a DOS dir to a file.
    >
    > What I want is to have perl remove the times (and preferably dates) from
    > this file.
    >
     
    Ben Liddicott, Nov 28, 2003
    #2
    1. Advertising

  3. Paul Tomlinson wrote:
    > All I have a file called "DirList.txt" which was generated by
    > redirecting the results of a DOS dir to a file.
    >
    > What I want is to have perl remove the times (and preferably dates)
    > from this file.


    If it's not too late, and to create a file with only the file names,
    wouldn't something like this be better:

    open FH, '> DirList.txt' or die $!;
    opendir DIR, '.' or die $!;
    print FH join "\n", readdir DIR;
    closedir DIR or die $!;
    close FH or die $!;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2003
    #3
  4. Thanks i've sorted it Bernards answer more-or-less sorted me out.
    "Gunnar Hjalmarsson" <> wrote in message
    news:bq7cs0$1t9mbe$-berlin.de...
    > Paul Tomlinson wrote:
    > > All I have a file called "DirList.txt" which was generated by
    > > redirecting the results of a DOS dir to a file.
    > >
    > > What I want is to have perl remove the times (and preferably dates)
    > > from this file.

    >
    > If it's not too late, and to create a file with only the file names,
    > wouldn't something like this be better:
    >
    > open FH, '> DirList.txt' or die $!;
    > opendir DIR, '.' or die $!;
    > print FH join "\n", readdir DIR;
    > closedir DIR or die $!;
    > close FH or die $!;
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    >
     
    Paul Tomlinson, Nov 28, 2003
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Paul Tomlinson" <> wrote in news:bq77c5$1vbnc1$1
    @ID-116287.news.uni-berlin.de:

    > All I have a file called "DirList.txt" which was generated by redirecting
    > the results of a DOS dir to a file.
    >
    > What I want is to have perl remove the times (and preferably dates) from
    > this file.


    On my windows 2000 computer, "dir /b" gives me just a bare list of
    filenames. I don't know if this option is available on all versions of
    dos/windows, but if it is, it'll be easier for you to change your input
    than to post-process it with Perl.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP8dcUWPeouIeTNHoEQIoIACgmZ7xcE1mBTwjjnrsI8CsBpNtrRIAn1AW
    xO8vdGMYS3Ab7M1J1M6OIH/b
    =5n/i
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Nov 28, 2003
    #5
  6. Eric J. Roode wrote:
    > On my windows 2000 computer, "dir /b" gives me just a bare list of
    > filenames. I don't know if this option is available on all
    > versions of dos/windows, but if it is, it'll be easier for you to
    > change your input than to post-process it with Perl.


    I tested on W98, and it worked there, too.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2003
    #6
  7. On Fri, 28 Nov 2003 10:16:15 -0000, "Paul Tomlinson"
    <> wrote:

    >All I have a file called "DirList.txt" which was generated by redirecting
    >the results of a DOS dir to a file.
    >
    >What I want is to have perl remove the times (and preferably dates) from
    >this file.


    perl -lpi.bak -e "s|\d{2}/d{2}/\d{2}\s+\d+.\d{2}||" DirList.txt

    or some reasonable variation. But isn't it that you really needed

    dir /b

    >c:\perl\bin\perl.exe -e tr/09:36//; "c:\DirList.txt"
    >
    >I know that this will only translate the time 09:36 to null but for now
    >anything is a start.


    NO! It will do something different.


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Nov 28, 2003
    #7
  8. On Fri, 28 Nov 2003 08:30:57 -0600, "Eric J. Roode"
    <> wrote:

    >On my windows 2000 computer, "dir /b" gives me just a bare list of
    >filenames. I don't know if this option is available on all versions of
    >dos/windows, but if it is, it'll be easier for you to change your input
    >than to post-process it with Perl.


    It exists at least since MS-DOS 5.0, that is the first version I
    "worked" with.


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Nov 29, 2003
    #8
  9. Paul Tomlinson

    Dave Weaver Guest

    On Fri, 28 Nov 2003 11:11:15 -0000, Ben Liddicott
    <> inconsiderately top-posted:
    >
    > The translation operator:
    >
    > tr/09:36//
    >
    > will remove all "0", "9", ":", "3" and "6" characters.


    No it won't.

    [davew]$ perl
    $x = "12:34:56";
    $x =~ tr/09:36//;
    print "x = $x\n";
    __END__
    x = 12:34:56
    [davew]$

    "perldoc perlop" for more info.
     
    Dave Weaver, Dec 1, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    885
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    216
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    217
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    217
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    171
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page