Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date question 3

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
Hi,

If I am writing an awk script, how do I append the current date a file I am outputting to i.e.

print var > "/tmp/out.log"

I know I can't use
export DATE=`date +%b%d%Y`

as in ksh, what are my options in awk?

stu.
 
Stu:

you can get the system date into an awk script by piping into getline:

"date"|getline var

Regards,

Ed
 
i had to :
Code:
system("date > /tmp/datefile");
getline date < &quot;/tmp/datefile&quot;;
system(&quot;rm /tmp/datefile&quot;);
when i did it ...
 
Thanks Olden,

How would I assign this var,
ie.

&quot;date&quot;|getline var

to the output log?

I've tried
> &quot;/tmp/log_var.log&quot;

this does not work, I believe because of the double quotes.... any ideas?
 
Try:

nawk ' {
&quot;date&quot;|getline var

print var >> &quot;log.file&quot;
} '
 
Set the file name by using

fn = &quot;/tmp/log_&quot; var &quot;.log

and write to it with

print ... > fn CaKiwi
 
is:
Code:
#!/bin/sh

TODAY=`date &quot;+%Y%m%d&quot;`
awk '{<script>}' > /tmp/log_${TODAY}.log
what you're after?
 
In case anyone's interested....

you can do

&quot;date +%b%d%Y&quot;|getline datevar
fn = &quot;/tmp/&quot;datevar

then
> fn

this will stamp the log in a easier to read fashion.

ie

Oct312002

Later,
Stu
 
Hi stu78:
Try the following:

Date=`date +%d%m%y`
<do your awking here> | outputname.log.$Date
# The above will assign current date to the variable Date, you can call the variable anywhere in your script.
#the output will look like this outputname.log.103102
#(assumming you run it today). ^^^^^^^^^^^^^^^^^^^^^^^^^^
Experience is the Best Teacher
But its' cost is Heavy!
^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top