如何在sed中更改日期格式? [英] How to change date format in sed?

查看:249
本文介绍了如何在sed中更改日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想通过 date 命令将时间戳中的日期转换为另一种格式。在控制台中,我会说 date -d @< timestamp> ,但我恰好想对文本文件中的许多字段执行此操作。



使用 e sed sed(GNU sed)4.2.2 ,实际上)我在说:

  $ echo 1449158360 | sed -r的#。*([0-9] {10})。*#date -d @ \1+%Y;#e'
2015

它的工作,很好!



现在我创建了一个虚拟文件 myfile

 我的时间戳是1449158360,但我也想知道什么日期是1359199960. 

我想替换为相同但具有时间戳的相对年份:

 我的时间戳是2015年,但也不知道2013年是哪个日期。

但是,如果我尝试运行与上述相同的命令,它将失败:

  $ sed -r's#([0-9] {10})#date -d @\1+%Y;#e'myfile 
sh:my:command not found
sh:but:command not found

因为 sed 将第一个单词解释为要执行的内容。



显然,如果我只是获取这些数据,而没有其他的东西,那么它可以工作:

  $ sed -r的#。*([0-9] {10})。*#date  - d @\1+%Y;#ge'myfile 
2015

所以我想知道,在 sed 中捕获的组中,我应该如何调用 date 并替换文本,考虑它被其他文字包围,不得不保持不变?

解决方案

e 切换到 sed sh -c 替换为不匹配的文本,并从此命令中显而易见:

  echo'a 1449158360'| sed -r的#([0-9] {10})#date -d @ \1+%Y;#e'
sh:a:command not found

所以即使我们只匹配 1449158360 sh -c 正在运行 a 1449158360



由于缺少这个解决方法正则表达式可能会显得很疯狂,但这是您可以如下所示从文件中运行多个匹配输入:



  sed -r's#(([^ 0-9] [0-9] {0,9})*)(\ b [0-9] {10} \b)(([0-9] {0,9} [^ 0-9])*)#printf%s%s%s\1$ (日期-d @ \3+%Y)\4;#ge'文件





基本上,我们在这个正则表达式中匹配<之前> 10位数> strong>输出:

 我的时间戳是2015年,但也不知道2013年的日期是多少。

为了澄清所用的正则表达式,我创建了此演示



绝对不是这个通用解决方案 e 模式问题,将其视为基于正则表达式的解决方法。


Say I want to convert a date in timestamp to another format through the date command. In console I would say date -d@<timestamp> but I happen to want to do this to many fields in a text file.

Using the e to execute in sed (sed (GNU sed) 4.2.2, actually) I am saying:

$ echo 1449158360 | sed -r 's#.*([0-9]{10}).*#date -d@\1 "+%Y";#e'
2015

It works, nice!

Now I created a dummy file myfile:

my timestamp is 1449158360 but also I wonder what date was 1359199960.

Which I would like to have replaced to the same but having the relative year of the timestamps:

my timestamp is 2015 but also I wonder what date was 2013.

However, if I try to run the same command as above it fails:

$ sed -r 's#([0-9]{10})#date -d@"\1" "+%Y";#e' myfile
sh: my: command not found
sh: but: command not found

Because sed interprets the first words as something to execute.

Obviously it works if I just fetch these data and nothing else:

$ sed -r 's#.*([0-9]{10}).*#date -d@"\1" "+%Y";#ge' myfile
2015

So I wonder: what should I do to call date against captured groups in sed and replace text with it, considering it is surrounded by other text that have to remain untouched?

解决方案

e switch in sed substitute applies sh -c to unmatched text as well as evident from this command:

echo 'a 1449158360' | sed -r 's#([0-9]{10})#date -d@\1 "+%Y";#e'
sh: a: command not found 

So even though we are matching only 1449158360 but sh -c is being run on a 1449158360.

Due to absence of non-greedy and lookaheads regex in sed this workaround regex might appear crazy but this is how you can run it for multiple matching input from file as in your question:

sed -r 's#(([^0-9][0-9]{0,9})*)(\b[0-9]{10}\b)(([0-9]{0,9}[^0-9])*)#printf "%s%s%s" "\1" $(date -d@\3 "+%Y") "\4";#ge' file

Basically we are matching <before>10-digits<after> in this regex.

Output:

my timestamp is 2015 but also I wonder what date was 2013.

To clarify the regex used I have created this demo.

By no means this is a generic solution to e mode issue, treat it as a regex based workaround.

这篇关于如何在sed中更改日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆