Perl 正则表达式更改日期格式
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用替换运算符
s///
:You can use the substitution operator
s///
:这是一个简单的脚本,它将采用输入和输出目录的可选参数。
使用
opendir
而不是 glob 将为您节省清理文件名的麻烦。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.