bash:将isodate转换为d:m:y

我有一个程序,它产生这样的输出(ISO格式date):

2010-06-04 15:48:17 +0200 2010-06-04 12:34:39 +0200 2010-02-01 14:02:44 +0200 2010-06-04 12:21:19 +0200 2010-06-04 12:21:04 +0200 2009-05-02 15:38:14 +0200 2009-03-02 15:38:09 +0200 2010-06-04 14:45:00 +0200 ... 

如何将输出转换为唯一的sortingdate:

 2010-06-04 2010-02-01 2009-05-02 2009-03-02 

然后,在每个独特的date运行: mycommand --withargs [yyyy-mm-dd]

您可以执行以下操作:

 $ <iso_command> | awk '{print $1}' | sort -u | xargs -n 1 mycommand --withargs 

如果您将要从海湾时区获取数据,使用简单的文本parsing工具可能不是最好的方法。

将格式化的string过滤到date命令可能会更好,然后使用它重新格式化,但是您喜欢。 简单的awk或cut可能会返回不完全准确的结果。 例如,如果您在PST中的位置,但是您获得的数据是UTC,那么在数据过程中,date将会在24小时之内closures。

 $echo '2010-06-04 15:48:17 +0200' | xargs -I var_d date --date='var_d' +%Y-%m-%d 2010-06-04 

cut -c 1-10 -|sort -ru

要么

cut -c 1-10 /path/to/myfile |sort -ru

假设从GNU coreutils的date

 program > dates while read isodate; do ymd=$(date -d "$isodate" +'%Y-%m-%d') mycommand --withargs "$ymd" done < dates