在mawk中使用strftime函数

发布于 2024-09-19 10:16:07 字数 276 浏览 5 评论 0原文

我正在尝试创建 AWK 脚本,该脚本将根据某种模式过滤输入文件,并使用 strftime() 函数进行一些计算。

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

使用的解释器是mawk。 使用此命令触发此脚本时:

awk -f script3 inputFile

我收到错误:“函数 strftime 从未定义”

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.

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

The interpreter in use is mawk.
When triggering this script using this command:

awk -f script3 inputFile

I'm getting the error: "function strftime never defined"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬 2024-09-26 10:16:07

安装 GAWK 将使您恢复在 awk 中本机缺少的函数 strftime。

使用 Ubuntu 11.10 或类似发行版,您将发出以下命令来获取 GAWK

sudo apt-get install gawk

您也可以在 gawk 中使用它,但您不必这样做,只需继续使用原始版本即可awk 脚本。

Installing GAWK will get you the function strftime back that you are missing natively in awk.

With Ubuntu 11.10 or simiar distribution you would issue following command to get the 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.

分开我的手 2024-09-26 10:16:07

好吧,显然, mawk 没有strftime 函数。

我这里没有 mawk,所以未经测试:

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

script 有(结合两个正则表达式:

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

如果年份应该以某种方式来自 $0,那么您应该能够从字符串中解析它。向我们提供有关您的编辑的更多详细信息

输入由几行组成,如下所示:“12768 Ashwari F 20 11 1985”。
基本上我必须过滤所有名称以 B 或 H 开头并以
名词此外,我必须计算每个过滤学生的年龄并找出
整个群体的平均年龄。

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

Well, clearly, mawk does not have a strftime function.

I don't have mawk here, so untested:

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

and script has (combining the two regular expressions:

$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. 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".
Basically I have to filter all those with a name that begins with B or H and ends with
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
是你 2024-09-26 10:16:07

您可以构建自己的日期实用程序包装器来提取所有组件。从那里,你可以按照你喜欢的方式进行制作:

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:~}

在这里,我还添加了一个功能来自动检测它是BSD-date还是GNU-date。根据您在系统中的命名方式进行相应调整。

这样,您就不需要进行一堆迄今为止的调用 - 只需调用一次,然后提取您认为合适的组件。

如果您想将单行概念发挥到极致,请将所有内容放入 sprintf() 语句中,如下所示:

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

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

1648666841181253

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])?
quot;)))\
       " +\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. 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.

And if you like to take one-liner concept to the extreme, then put everything inside a sprintf() statement like this :

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

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

1648666841181253
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文