如何将隐含年份添加到未指定年份的字符串中?
我必须从网站下载许多文本文件。然后我必须将其放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个有点高尔夫的 Perl 解决方案:
A little golfy perl solution:
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'