Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by shivaa; 12-13-2012 at 05:11 AM.
Reason: ---- ADDITION -------
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:
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..>
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..>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.