LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printing date in awk/nawk (http://www.linuxquestions.org/questions/linux-newbie-8/printing-date-in-awk-nawk-4175441279/)

shivaa 12-13-2012 05:59 AM

Printing date in awk/nawk
 
Hello everyone!
I am facing a challenge while printing date in a awk code.
I have either awk or nawk, but not gawk. So function like strftime isn't working with awk/nawk.
Following is code:
Code:

awk 'BEGIN{
format = "%v"; # Or I use, format = "%d/%b/%Y";
val1=val2=0;
<some_code>
<some_code>
END{ print strftime(format), val1, val2"
}' file1

Code is working fine, except printing the date, and it's throwing errors all the time, so is there any alternative available in awk/nawk like strftime to print date in desired format?
I want it to print like: 13/Dec/2012

------- ADDITION -----------
I tested a simple code, in both awk/nawk (on Solaris) and gawk (on Linux).
Code:

#!/usr/bin/nawk -f  # Or /bin/gawk -f in Linux
BEGIN{
format = "%d/%b/%Y";
print strftime(format);
}

It's working ok with gawk, but not with awk/nawk i.e. in Solaris.

druuna 12-13-2012 06:16 AM

You are trying to use gawk specific functions. This from the OReilly sed/awk book:
Quote:

Gawk has one additional string function, and two functions for dealing with the current date and time. They are listed in Table 11.9.
Table 11.9: Additional gawk Functions

gensub(r, s, h, t)
If h is a string starting with g or G, globally substitutes s for r in t. Otherwise, h is a number: substitutes for the h'th occurrence. Returns the new value, t is unchanged. If t is not supplied, defaults to $0.

systime()
Returns the current time of day in seconds since the Epoch (00:00 a.m., January 1, 1970 UTC).

strftime(format, timestamp)
Formats timestamp (of the same form returned by systime()) according to format. If no timestamp, use current time. If no format either, use a default format whose output is similar to the date command.

An alternative would be the system function that uses the date <format> command:
Code:

system("date '+%c'")

shivaa 12-13-2012 07:15 AM

Thanks Druuna, but it additionally printing command status with date. For example I tried:
Code:

#!/usr/bin/nawk -f
BEGIN{
system("date '+%d/%b/%Y'");
print system();

But output is like:
Code:

13/Dec/2012
0

So how to avoid this 0?

On the other hand, is it possible to use a variable, which is outside of awk code, but is inside the script which contains the awk code, like:
Code:

#!/bin/bash
day=%Y%m%d  # Variable defined in script
<some_part_of_script>

# awk code starts
/use/bin/nawk -f
<awk_code>
print day # Is such kind of use of external variable possible?
# awk code ends

<script_continues..>


druuna 12-13-2012 07:30 AM

Quote:

Originally Posted by shivaa (Post 4848410)
Thanks Druuna, but it additionally printing command status with date. For example I tried:
Code:

#!/usr/bin/nawk -f
BEGIN{
system("date '+%d/%b/%Y'");
print system();

But output is like:
Code:

13/Dec/2012
0

So how to avoid this 0?

The system("date '+%d/%b/%Y'") prints 13/Dec/2012 and the print system() prints 0. The second command isn't needed (and syntactically incorrect; No command is given to system()).

Quote:

On the other hand, is it possible to use a variable, which is outside of awk code, but is inside the script which contains the awk code, like:
Code:

#!/bin/bash
day=%Y%m%d  # Variable defined in script
<some_part_of_script>

# awk code starts
/use/bin/nawk -f
<awk_code>
print day # Is such kind of use of external variable possible?
# awk code ends

<script_continues..>


Have a look at this:
Code:

#!/bin/bash

day="$(date '+%d/%b/%Y')"

awk -v day="$day" 'BEGIN { print day }' infile


shivaa 12-13-2012 08:01 AM

Many thanks @druuna! Both solutions worked fine, I'd prefer the second one indeed.
One last question:
If I use:
Code:

system("date '+%d/%b/%Y'");
print var1, "\t", var2
OR
system("date '+%d/%b/%Y'"); print var1, "\t", var2

In both cases, it print date and values in different lines, like:
Code:

13/Dec/2012
12345    54321

But I want,
Code:

13/Dec/2012    12345    54321
I tried some no newline options, but nothing worked... could you help?

druuna 12-13-2012 08:38 AM

Have a look at this (no system() used):
Code:

awk 'BEGIN { "date '+%d/%b/%Y'" | getline date ; var1 = 1000 ; var2 = 2000 } END { print date, "\t", var1, "\t", var2 }' infile
13/dec/2012      1000    2000

I think I would prefer:
Code:

awk -v date=$(date '+%d/%b/%Y') 'BEGIN{ var1 = 1000 ; var2 = 2000 } END { print date, "\t", var1, "\t", var2 }' infile
13/dec/2012      1000    2000


shivaa 12-13-2012 09:39 AM

Many thanks @druuna!! You always comes as a savior! Thanks again.


All times are GMT -5. The time now is 10:49 PM.