LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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



Reply
 
Search this Thread
Old 12-13-2012, 05:59 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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.

Last edited by shivaa; 12-13-2012 at 06:11 AM. Reason: ---- ADDITION -------
 
Old 12-13-2012, 06:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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'")
 
1 members found this post helpful.
Old 12-13-2012, 07:15 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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..>

Last edited by shivaa; 12-13-2012 at 07:17 AM.
 
Old 12-13-2012, 07:30 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by shivaa View Post
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
 
2 members found this post helpful.
Old 12-13-2012, 08:01 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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?
 
Old 12-13-2012, 08:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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
 
2 members found this post helpful.
Old 12-13-2012, 09:39 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Many thanks @druuna!! You always comes as a savior! Thanks again.
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using BASH to Rename files with last mod date using stat & awk Liquid_Squelch Programming 4 12-11-2011 07:27 PM
[SOLVED] Converting the contents of a column with the output of the date command using awk mystupidquestion Programming 4 10-02-2011 05:22 PM
awk/nawk returning decimal values? moutaye Linux - General 6 03-25-2008 06:16 PM
awk / nawk question - hostname dazdaz Programming 4 03-07-2008 04:50 PM
date in awk raj_deep2k1 Linux - Networking 5 01-21-2006 09:09 AM


All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration