将日期 (month_name DD, YYYY) 转换为 (YYYY-MM-DD) 的脚本
我有一个文本文件,其日期格式为:“date=month_name DD, YYYY”和“date=(month_name DD, YYYY)”
如何转换这些日期,使其采用以下形式:“date=YYYY-MM- DD”?
我还有一些日期前面带有字段名称“accessdate=”或没有字段名称,我想转换它们。
谢谢。
附录:
I have a text file with dates in the form: "date=month_name DD, YYYY" and "date=(month_name DD, YYYY)"
How can I convert these dates so they are in the form: "date=YYYY-MM-DD"?
I also have some dates preceded by the field name "accessdate=" or no field name, that I would like to convert.
Thanks.
ADDENDUM:
- The month names are are the full English month names e.g. January, February, etc.
- I would only like to convert the dates inside ref tags i.e. they would be surrounded by other text inside
<ref></ref>'
- I'm open to any language for the scripting. I've done a little bash, javascript & python. But I think awk, sed, perl, etc. would be also fine. Explanations of the code would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您使用的工具。
例如使用 awk & sed 你可以做这样的事情:
然后你可以做一个
或者你可以用纯 awk 编写它,通过解析 $0.
Depends on the tool you use.
E.g. with awk & sed you can do something like this:
Then you can do an
Or you can write it in pure awk, by parsing $0.
您可以从
要处理“date=(month_name DD, YYYY)”开始,您还可以添加 sed 's/date=(\([^(]*\))/date=\1/'
关于您的附录,如果它跨越一行以上,则 sed 不足以使用
标记。例如Python,
可以使用
re.search()
来查找和匹配的
。然后,
re.match()
可用于使用类似于 sed 中使用的正则表达式来转换内部内容。该算法必须包含在while
循环中才能遍历。所有文件。You can begin with
To handle "date=(month_name DD, YYYY)" you can also add
sed 's/date=(\([^(]*\))/date=\1/'
to the pipe and so on.Concerning your addendum. sed would not be enough to work with
<ref></ref>
tag if it spans more then one line. So you have to use something more powerful. E.g. Python.re.search()
can be used to find<ref>
and the matching</ref>
. Thenre.match()
can be used to transform what's inside using the regexps similar to those used in sed. This algorithm have to be enclosed in awhile
loop to traverse all the document.