convert month from Aaa to xx in little script with awk

后端 未结 4 437
执笔经年
执笔经年 2021-01-14 01:27

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|uni         


        
相关标签:
4条回答
  • 2021-01-14 01:59

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

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

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

    0 讨论(0)
  • 2021-01-14 02:03

    Just use the field number of your month as an index into the months array.

    print months[$6]
    

    Since ls output differs from system to system and sometimes on the same system depending on file age and you didn't give any examples, I have no way of knowing how to guide you further.

    Oh, and don't parse ls.

    0 讨论(0)
  • 2021-01-14 02:04

    Assuming the name of the months only appear in the month column, then you could do this:

    ls -la foo*.bar|awk '{sub(/Jan/,"01");sub(/Feb/,"02");print $7, $6}'|sort|uniq -c
    
    0 讨论(0)
  • 2021-01-14 02:11

    To parse AIX istat, I use:

    istat .profile | grep "^Last modified" | read dummy dummy dummy  mon day time dummy yr dummy
    echo "M: $mon D: $day T: $time Y: $yr"
    -> Month: Mar Day: 12 Time: 12:05:36 Year: 2012
    

    To parse AIX istat month, I use this two-liner AIX 6.1 ksh 88:

    monstr="???JanFebMarAprMayJunJulAugSepOctNovDec???"
    mon="Oct" ; hugo=${monstr%${mon}*} ; hugolen=${#hugo} ; let hugol=hugolen/3 ; echo "Month: $hugol"
    -> Month: 10
    

    1..12 : month name ok

    If lt 1 or gt 12 : month name not ok

    Instead of "hugo" use speaking names ;-))

    0 讨论(0)
提交回复
热议问题