1820882 Members
3415 Online
109628 Solutions
New Discussion юеВ

Dates in awk Scripts

 
SOLVED
Go to solution
Carla L. Thompson
New Member

Dates in awk Scripts

I'm trying to create a Header Record with a Date Stamp, and I can't figure out the syntax:

The following doesn't work:

BEGIN {
header_type = "H"
header_desc = "Display Name of File"
header_date = strftime("%y%m$d",systime())
header_filler = "||||"

print header_type "|" header_desc "|" header_date "|" header_filler
}

{
}
END {
}

Suggestions welcomed!
3 REPLIES 3
John Poff
Honored Contributor
Solution

Re: Dates in awk Scripts

Hi,

I couldn't get it to work on HP-UX 11.11. My awk complains about the strftime function. I tried it on a Linux box and it seems to know about that function, and it seemed to work when I changed this line:

header_date = strftime("%y%m$d",systime())

to this:

header_date = strftime("%y%m%d",systime())

It seems happier with the '%d' instead of the '$d', and gives back something like this:

H|Display Name of File|040818|||||

Is that what you had in mind?

JP
curt larson_1
Honored Contributor

Re: Dates in awk Scripts

well strftime is a library function and isn't an awk command, so that isn't going to work.

what will probably work for is the getline command used as: command | getline variable

as your command use date. see the man page for more information

of course you could also pass the date as a variable, -v date=$(date) '{...
or as part of the environment ENVIRON["date"]
Carla L. Thompson
New Member

Re: Dates in awk Scripts

Yes, that's exactly what I needed... when I tried that here on my OS... I received the following error message:

nawk: calling undefined function strftime
source line number 10

So, there's something odd about my ability to recognize functions.

I'm currently using the following code:

"date +'%Y%m%d'"| getline header_date

I really appreciate your input and suggestion!