Reputation: 111
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"
Upvotes: 11
Views: 18628
Reputation: 2821
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
. 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
Upvotes: 0
Reputation: 149
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.
Upvotes: 14
Reputation: 246837
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
Upvotes: 3