How do I use output from awk in another command?

倾然丶 夕夏残阳落幕 提交于 2019-12-08 15:58:32

问题


So I need to convert a date to a different format. With a bash pipeline, I'm taking the date from the last console login, and pulling the relevant bits out with awk, like so:

last $USER | grep console | head -1 | awk '{print $4, $5}'

Which outputs: Aug 08 ($4=Aug $5=08, in this case.)

Now, I want to take 'Aug 08' and put it into a date command to change the format to a numerical date.

Which would look something like this:

date -j -f %b\ %d Aug\ 08 +%m-%d

Outputs: 08-08

The question I have is, how do I add that to my pipeline and use the awk variables $4 and $5 where 'Aug 08' is in that date command?


回答1:


You just need to use command substitution:

date ... $(last $USER | ... | awk '...') ...

Bash will evaluate the command/pipeline inside the $(...) and place the result there.




回答2:


Get awk to call date:

... | awk '{system("date -j -f %b\ %d \"" $4 $5 "\" +%b-%d")}'

Or use process substitution to retrieve the output from awk:

date -j -f %b\ %d "$(... | awk '{print $4, $5}')" +%b-%d



回答3:


I'm guessing you already tried this?

last $USER | grep console | head -1 | awk | date -j -f %b\ %d $4 $5 +%b-%d



回答4:


Using back ticks should work to get the output of your long pipeline into date.

date -j -f %b\ %d \`last $USER | grep console | head -1 | awk '{print $4, $5}'\` +%b-%d


来源:https://stackoverflow.com/questions/3452339/how-do-i-use-output-from-awk-in-another-command

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