如何将隐含年份添加到未指定年份的字符串中?

发布于 2024-10-18 17:47:20 字数 341 浏览 2 评论 0原文

我必须从网站下载许多文本文件。然后我必须将其放入 MySQL 数据库中,但该文件具有以下形式的行:

02/04 15:00 Some strings

03/03 15:00 other strings

01/12/2010 12:00 other strings

03/04 15:00 more strings

...

当年份未明确写入时,这意味着它是当前年份。因此,我需要逐行解析文件,并将 dd/mm 形式的每个日期转换为 dd/mm/yyyy 形式的日期(其中 yyyy 是当然是当年)在我将其放入数据库之前。
我该怎么做?

I have to download many text files from a website. Then I have to put it into a MySQL database, but the file has lines of the form:

02/04 15:00 Some strings

03/03 15:00 other strings

01/12/2010 12:00 other strings

03/04 15:00 more strings

...

When the year is not explicitly written, it means that it is the current year. So I need to parse the file line by line and convert every date of the form dd/mm to a date of the form dd/mm/yyyy (where yyyy is the current year of course) before I put it into database.
How can I do this?

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

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

发布评论

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

评论(5

你怎么这么可爱啊 2024-10-25 17:47:20

一个有点高尔夫的 Perl 解决方案:

perl -MTime::Piece -pe '$yy=localtime->year; s{^(\d{2}/\d{2})(\s)}{$1/$yy$2}' input

A little golfy perl solution:

perl -MTime::Piece -pe '$yy=localtime->year; s{^(\d{2}/\d{2})(\s)}{$1/$yy$2}' input
债姬 2024-10-25 17:47:20
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

my $pattern = qr(^(\d\d/\d\d) );#month/day at start of line followed by space
my $year = localtime->year;

while (<DATA>){
    s/$pattern/$1\/$year /;
    print;
}
__DATA__
02/04 15:00 Some strings
03/03 15:00 other strings
01/12/2010 12:00 other strings
03/04 15:00 more strings
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

my $pattern = qr(^(\d\d/\d\d) );#month/day at start of line followed by space
my $year = localtime->year;

while (<DATA>){
    s/$pattern/$1\/$year /;
    print;
}
__DATA__
02/04 15:00 Some strings
03/03 15:00 other strings
01/12/2010 12:00 other strings
03/04 15:00 more strings
離殇 2024-10-25 17:47:20
year=`date +%Y`
sed "s|^\([0-9][0-9]/[0-9][0-9]\) |\1/$year |" filename
year=`date +%Y`
sed "s|^\([0-9][0-9]/[0-9][0-9]\) |\1/$year |" filename
独自唱情﹋歌 2024-10-25 17:47:20

awk -v 年=$(日期 +%Y) 'match($1, "^[0-9][0-9]/[0-9][0-9]$") {$1 = $1 "/"year} 1'

awk -vyear=$(date +%Y) 'split($1, a, "/") == 2 {$1 = $1"/"year} 1'

awk -v year=$(date +%Y) 'match($1, "^[0-9][0-9]/[0-9][0-9]$") {$1 = $1"/"year} 1'

or

awk -v year=$(date +%Y) 'split($1, a, "/") == 2 {$1 = $1"/"year} 1'

给我一枪 2024-10-25 17:47:20
$ awk 'BEGIN{y=strftime("%Y")}length($1)==5{$1=$1"/"y}1' file
02/04/2011 15:00 Some strings
03/03/2011 15:00 other strings
01/12/2010 12:00 other strings
03/04/2011 15:00 more strings

$ ruby -ane '$F[0].size==5 && $F[0]=$F[0]+"/"+Time.now.year.to_s;puts $F.join(" ")' file
$ awk 'BEGIN{y=strftime("%Y")}length($1)==5{$1=$1"/"y}1' file
02/04/2011 15:00 Some strings
03/03/2011 15:00 other strings
01/12/2010 12:00 other strings
03/04/2011 15:00 more strings

$ ruby -ane '$F[0].size==5 && $F[0]=$F[0]+"/"+Time.now.year.to_s;puts $F.join(" ")' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文