转换月份,从Aaa级与AWK的小脚本到xx [英] convert month from Aaa to xx in little script with awk

查看:139
本文介绍了转换月份,从Aaa级与AWK的小脚本到xx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对每个日期创建的文件的数量的报告。我可以做到这一点与这个小单行:

I am trying to report on the number of files created on each date. I can do that with this little one liner:

ls -la foo*.bar|awk '{print $7, $6}'|sort|uniq -c

和我得到很多fooxxx.bar文件是如何被创建日期列表,但当月的格式为:AAA(即:4月),我想XX(即:04)。

and I get a list how many fooxxx.bar files were created by date, but the month is in the form: Aaa (ie: Apr) and I want xx (ie: 04).

我觉得答案就在这里:

awk '
BEGIN{
   m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|")
   for(o=1;o<=m;o++){
      months[d[o]]=sprintf("%02d",o)
    }
format = "%m/%d/%Y %H:%M"
}
{
split($4,time,":")
date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0")
print strftime(format, mktime(date))
}'

但无权很少知道我需要剥离出来,不知道怎么打发$ 7无论我能开出的这四月转换为04。

But have no to little idea what I need to strip out and no idea how to pass $7 to whatever I carve out of this to convert Apr to 04.

谢谢!

推荐答案

下面是一个缩写的月份名称转换为数字在awk中的惯用方式:

Here's the idiomatic way to convert an abbreviated month name to a number in awk:

$ echo "Feb" | awk '{printf "%02d\n",(match("JanFebMarAprMayJunJulAugSepOctNovDec",$0)+2)/3}'
02

$ echo "May" | awk '{printf "%02d\n",(match("JanFebMarAprMayJunJulAugSepOctNovDec",$0)+2)/3}'
05

让我们知道,如果你需要更多的信息来解决问题。

Let us know if you need more info to solve your problem.

这篇关于转换月份,从Aaa级与AWK的小脚本到xx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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