Perl 正则表达式更改日期格式

发布于 2024-11-27 05:02:58 字数 810 浏览 0 评论 0原文

我对 Perl 完全陌生,我需要完成一个小的查找和替换来更改一组大文件中的日期格式。 文件的日期格式为:dd.mm.yyyy 我需要将它们更改为:mm-dd-yyyy

如何使用 perl 执行此操作?

我有一个基本代码来读取目录中的文件并写入输出文件,我需要在 while 循环之间添加替换逻辑(如果我没有错的话!)。

#!c:/perl64/bin/perl.exe

#loop around a directory
@files = <C:/perl64/data/*>;

# loop around files
foreach $file (@files) {

#Read File
open READ, $file or die "Cannot open $read for read :$!";

#Output File
$fname=substr($file, rindex($file,"/")+1,length($file)-rindex($file,"/")-1);
$write="C:/perl64/output/$fname";

open WRITE, ">$write" or die "Cannot open $write for write :$!";

#Loop Around file
while (<READ>) {

           # TO DO: Change date format from dd.mm.yyyy to mm-dd-yyyy

           #Write to ourput file
           print WRITE "$_";

}

} 

问候,

阿南德

I'm totally new to Perl, and I need to get a small find and replace done to change the date format in a set of large file.
The files have dates in the format: dd.mm.yyyy
and I need to change them to: mm-dd-yyyy

How do I do this with perl?

I have a basic code to read through the files in a directory and write to an output file, I'll need to have my replace logic in-between the while loop (if i'm not wrong!).

#!c:/perl64/bin/perl.exe

#loop around a directory
@files = <C:/perl64/data/*>;

# loop around files
foreach $file (@files) {

#Read File
open READ, $file or die "Cannot open $read for read :$!";

#Output File
$fname=substr($file, rindex($file,"/")+1,length($file)-rindex($file,"/")-1);
$write="C:/perl64/output/$fname";

open WRITE, ">$write" or die "Cannot open $write for write :$!";

#Loop Around file
while (<READ>) {

           # TO DO: Change date format from dd.mm.yyyy to mm-dd-yyyy

           #Write to ourput file
           print WRITE "$_";

}

} 

Regards,

Anand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-04 05:02:58

您可以使用替换运算符s///

while (<READ>) {
    s/(\d{2})\.(\d{2})\.(\d{4})/$2-$1-$3/;
    print WRITE "$_";
}

You can use the substitution operator s///:

while (<READ>) {
    s/(\d{2})\.(\d{2})\.(\d{4})/$2-$1-$3/;
    print WRITE "$_";
}
人心善变 2024-12-04 05:02:58

这是一个简单的脚本,它将采用输入和输出目录的可选参数。

使用 opendir 而不是 glob 将为您节省清理文件名的麻烦。

use strict;
use warnings;
use autodie;

my $indir = shift || "C:/perl64/data";
my $outdir = shift || "C:/perl64/output";

opendir(my $in, $indir);

while (readdir $in) {
    next unless -f;
    open my $infile, '<', $_;
    open my $outfile, '>', $outdir . "/" . $_;
    while (<$infile>) {
        s/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/$2-$1-$3/g; 
        print $outfile $_;
    }
}

Here's a simple script that will take optional arguments for input and output directories.

The use of opendir instead of a glob will save you some trouble cleaning up the file names.

use strict;
use warnings;
use autodie;

my $indir = shift || "C:/perl64/data";
my $outdir = shift || "C:/perl64/output";

opendir(my $in, $indir);

while (readdir $in) {
    next unless -f;
    open my $infile, '<', $_;
    open my $outfile, '>', $outdir . "/" . $_;
    while (<$infile>) {
        s/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/$2-$1-$3/g; 
        print $outfile $_;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文