简体   繁体   English

在 mawk 中使用 strftime function

[英]Using strftime function in mawk

I'm trying to create AWK script that will filter the input file according to some pattern, and use the strftime() function for some calculations.我正在尝试创建 AWK 脚本,它将根据某种模式过滤输入文件,并使用 strftime() function 进行一些计算。

($2 ~ /^[HB]/ && $2 ~ /n$/){
        print strftime("%Y")
}

The interpreter in use is mawk.使用的解释器是 mawk。 When triggering this script using this command:使用此命令触发此脚本时:

awk -f script3 inputFile

I'm getting the error: "function strftime never defined"我收到错误:“函数 strftime 从未定义”

Installing GAWK will get you the function strftime back that you are missing natively in awk. 安装GAWK会让你获得strftime功能,因为你在awk中本身就缺少了这个功能。

With Ubuntu 11.10 or simiar distribution you would issue following command to get the GAWK 使用Ubuntu 11.10或simiar发行版,您可以发出以下命令来获取GAWK

sudo apt-get install gawk

You can also use it in gawk however you don't have to and can simply go ahead with your original awk script. 您也可以使用gawk,但是您不必使用它,并且可以简单地继续使用您的原始awk脚本。

Well, clearly, mawk does not have a strftime function. 嗯,显然, mawk没有strftime功能。

I don't have mawk here, so untested: 我没有这里的mawk,所以未经测试:

awk -f script -v the_year=$(date "+%Y") inputFile

and script has (combining the two regular expressions: script有(结合两个正则表达式:

$2 ~ /^[HB].*n$/ { print the_year }

If the year should come from $0 somehow, then you should be able to parse it out of the string. 如果年份应该以某种方式来自$ 0,那么你应该能够从字符串中解析它。 Give us more details about your input. 提供有关您输入的更多详细信息。

EDIT 编辑

the input is made of several rows that look like this: "12768 Ashwari F 20 11 1985". 输入由几行组成,如下所示:“12768 Ashwari F 20 11 1985”。 Basically I have to filter all those with a name that begins with B or H and ends with n. 基本上我必须过滤所有名称以B或H开头并以n结尾的名称。 In addition I have to calculate the age of each filtered student and find out the average age of the entire group. 此外,我必须计算每个过滤学生的年龄,并找出整个组的平均年龄。

awk -v this_year=$(date +%Y) -v today=$(date +%Y%m%d) '
    $2 ~ /^[BH].*n$/ {
        age = this_year - $6
        if (today < $6 $5 $4) { age-- } # I assume those fields are the birthday 
        total_age += age
        count ++
        print $2 " is " age " years old"
    }
    END {
        print "average age = " total_age/count
    }
' inputFile

you can build your own date utility wrapper to extract out all the components.您可以构建自己的日期实用程序包装器来提取所有组件。 from there, you can make it however you like it:从那里,你可以随心所欲地制作它:

echo 123 abc \
 \
 | mawk 'function dateinfo(_,__,___) { 

     ___=RS
         RS="\n"

    (_=(_=substr("gdate",2^(_!~"^[Gg]([Nn][Uu])?$")))\
       " +\47-"(_)"-:%Y:%m:%d:~:%B:~:-weeknum-:%U:"\
     "-julian-:%-j:-dayofweek-:~:%A:%w:-time-:~:%T:"\
     "~:-epochs-:~:%s:"(_~"^g"?"%N":"000000000")":~:"\
                             "-timezone-:~:%z:%Z:~\47") | getline __;
      return \
              \
      printf("%.*s%s",close(_)<"",RS=___,__) 
  } { 
       printf("\n{%s}\n\t{%s}\n\n\t{%s}\n\n",
               $0,dateinfo(),dateinfo("gnu")) }'

{123 abc}
        {-date-:2022:03:30:~:March:~:-weeknum-:13:-julian-:
         89:-dayofweek-:~:Wednesday:3:-time-:~:14:36:04:~:
         -epochs-:~:1648665364:000000000:~:-timezone-:~:-0400:EDT:~}
       
        {-gdate-:2022:03:30:~:March:~:-weeknum-:13:-julian-:
         89:-dayofweek-:~:Wednesday:3:-time-:~:14:36:04:~:
         -epochs-:~:1648665364:208758000:~:-timezone-:~:-0400:EDT:~}

Here I've made it also added in a feature to auto detect whether it's BSD-date or GNU-date .在这里,我还添加了一个功能来自动检测它是BSD-date还是GNU-date Adjust it accordingly to how you name it in your system.根据您在系统中的命名方式对其进行相应调整。

This way you don't need to make a stack of calls to date - just call it once, and extract the components as you see fit.这样你就不需要对 date 进行一堆调用 - 只需调用一次,然后提取你认为合适的组件。

And if you like to take one-liner concept to the extreme, then put everything inside a sprintf() statement like this:如果你喜欢将单行概念发挥到极致,那么将所有内容都放在 sprintf() 语句中,如下所示:

mawk2 'function usectime(_,__,___) {
                                    \
  return                             \
                                      \
  sprintf("%.0s%.0s%s",(__=substr((RS="\n"substr(\
         _="",_<(___=RS)))"gdate +%s%6N",!_+!_)) | getline _,
         close(__)^(RS=___),_)  

 } $!_=usectime()' <<<'' | lgp3

1648666841181253

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM