Pipe string to GNU Date for conversion - how to make it read from stdin?

冷暖自知 提交于 2019-12-30 07:58:09

问题


GNU Date lets you convert date strings like so:

$ date +"%d %m %Y" -d "yesterday"
  04 01 2012

Is it possible to pipe a date string to it for conversion? I've tried the obvious -d - like so:

$ echo "yesterday" | date +"%d %m %Y" -d -

but it prints today's date instead of yesterdays.

Is it possible to pipe values to it or doesn't it support that?

Thanks.


回答1:


Yes.

 echo "yesterday" | xargs date +"%d %m %Y" -d



回答2:


date -f tells it to do the same thing as -d except for every line in a file... you can set the filename to - to make it read from standard input.

echo "yesterday" | date +"%d %m %Y" -f -



回答3:


You can use `command` or $(command) substitution:

date +"%d %m %Y" -d $(echo "yesterday")



回答4:


Just to throw it in, in bash:

date +"%d %m %Y" -f <(echo yesterday)


来源:https://stackoverflow.com/questions/8742476/pipe-string-to-gnu-date-for-conversion-how-to-make-it-read-from-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!