The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract date with time from file prash_b Shell Programming and Scripting 5 06-18-2008 08:30 AM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 11:23 AM
how do I put a date and time in a file name jhamm UNIX for Dummies Questions & Answers 2 01-16-2007 09:31 AM
file creation date & time alisevA3 UNIX for Dummies Questions & Answers 3 07-08-2005 05:21 AM
File date and time stamp Xenon UNIX for Dummies Questions & Answers 1 10-09-2001 03:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2007
captoro captoro is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
Date and time log file

Hi,

I wrote a small perl script in unix that searches in a file and saves some information in a separate file. Since this is a log file, I would like to have the date added to file name. I have no idea where to start.
output:
log_010907.txt

thanks
ken
  #2 (permalink)  
Old 09-01-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
perldoc -q open
perldoc -f localtime

Last edited by ghostdog74; 09-02-2007 at 12:12 AM..
  #3 (permalink)  
Old 09-02-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
You can save the output of the date command (date "+%d%m%y") to a variable and append it to your log file name.

Hope this is what you were looking for.
  #4 (permalink)  
Old 09-03-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
you can do--

str1=`date "+%d%m%y"`

and use this variable in the logfilename.
log1_$str1.txt

Thanks
Namish
  #5 (permalink)  
Old 09-03-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
the OP has a perl script.
  #6 (permalink)  
Old 09-03-2007
captoro captoro is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 3
OP = operator ?

here is what I have from what you told me.
Code:
#!/usr/bin/perl -w
use Time::localtime;
 
str1=`date "+%d%m%y"`;
open (FILE,"<traffic_TLG");

foreach $line (<FILE>) {
  if ($line =~ m/beth/) {
  open (FILE, ">>log_$str1.txt");
  
    @items =  split(",",$line);
    print FILE $items[4], ",", $items[10], ",", $items[15], ";" . "\n";

	close(FILE); 
  }

}

close (FILE);
I get a cannot modify a constant scalar error when I run it. Otherwise the program works. why does'n it work?

Last edited by blowtorch; 09-03-2007 at 09:16 AM..
  #7 (permalink)  
Old 09-03-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
there's no need to use Time::localtime. from perldoc -f localtime
Code:
 
#  0    1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
from that, you can assign your date variable. there's no need to call external date command from the shell. for more information, read the rest of perldoc -f localtime.

put all your open() and close() commands outside the for loop. Use a different file handler, don't name both of them FILE !..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0