Linux 管道字符串到 GNU 日期进行转换 - 如何使其从标准输入读取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8742476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:55:50  来源:igfitidea点击:

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

linuxdatepipestdin

提问by Brian Beckett

GNU Date lets you convert date strings like so:

GNU Date 允许您像这样转换日期字符串:

$ 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:

是否可以通过管道将日期字符串传递给它进行转换?我试过这样明显的-d -

$ 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.

谢谢。

采纳答案by ?imon Tóth

Yes.

是的。

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

回答by praetorian droid

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

您可以使用`command`$(command)替换:

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

回答by praetorian droid

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

date-f告诉它做与-d文件中每一行不同的事情……你可以将文件名设置为-从标准输入中读取。

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

回答by Larry

Just to throw it in, in bash:

只是把它扔进去,在 bash 中:

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