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

P

Paul Tomlinson

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
 
B

Ben Liddicott

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
 
G

Gunnar Hjalmarsson

Paul said:
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 $!;
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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-----
 
G

Gunnar Hjalmarsson

Eric said:
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.
 
M

Michele Dondi

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
 
M

Michele Dondi

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
 
D

Dave Weaver

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.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top