Formatting date time in unix using perl breaks


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting date time in unix using perl breaks
# 1  
Old 11-09-2010
Lightbulb Formatting date time in unix using perl breaks

while read l
do
vTimeCreated=`perl -e '@d=localtime ((stat(shift))[9]); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' ${l}`
echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt
done < servername.txt
Using the above code to format date time for each of the filenames present in servername.txt. But it gets stuck in the perl area when huge content is there in the file. Is this a memory problem or is there any other way that we can do the formatting?
# 2  
Old 11-09-2010
Well, you fork and exec perl, a high level language processor, for every line in a shell script, a low level language processor. exec perl once and read the file there.
# 3  
Old 11-09-2010
Data

my desired output in the output file is <filename> || <date>. And the date is not the current date but the date in which the file was created. so i need to do that in the loop. Is there a way to flush the perl or any other better way to do it without using perl
# 4  
Old 11-09-2010
If you have GNU date the following will work

Code:
while read file
do
   echo "$file  || $(date -r $file +"%d-%m-%Y %H:%M:%S")"
done < servername.txt


--Edit--
If not do you have the stat (/usr/bin/stat) command available?

Last edited by Chubler_XL; 11-10-2010 at 12:00 AM.. Reason: Typo
# 5  
Old 11-10-2010
Data

my unix version doesnt support GNU or stat. Is there any other way? or atleast may be flush the perl section after the command is run?
# 6  
Old 11-12-2010
Quote:
Originally Posted by Chubler_XL
If you have GNU date the following will work

Code:
while read file
do
   echo "$file  || $(date -r $file +"%d-%m-%Y %H:%M:%S")"
done < servername.txt


--Edit--
If not do you have the stat (/usr/bin/stat) command available?
It is nice to find a solution without a fork/exec per line. I recall ksh93 has nice date time built-in functionality.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with Formatting date in Perl

Good day. I am trying to change the output on my date so that I can get the month as a numeric value. TODAY=`perl -e 'print localtime(time()) . "\n" '` Here are the results from the above perl statement Thu Feb 11 13:16:40 2016 I am not sure of the syntax to get a date more like... (2 Replies)
Discussion started by: ziggy6
2 Replies

2. Shell Programming and Scripting

UNIX Date Formatting

Hi I have a shell variable storing DATE in YYYY-MM-DD format is there a way i extract required field say only DD Also, would be great if there is a way which could take date format as well so that code is generic for any date format eg DDMMYYYY or DD/MM/YYYY or YYYY/MM/DD etc. Thanks (4 Replies)
Discussion started by: skyineyes
4 Replies

3. Emergency UNIX and Linux Support

DATE TIME formatting

can anyone one help me....to make date and time format...to following format for my file Code: DATE TIME DD- MON- YEAR 24 Hours I have a need of format like this 12-Jan-2012 in one column, then time in 24 Hours in another column....please help...me... ... (7 Replies)
Discussion started by: nex_asp
7 Replies

4. Shell Programming and Scripting

Adding time to date time in UNIX shell scipting

I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as... (3 Replies)
Discussion started by: codehelp04
3 Replies

5. Shell Programming and Scripting

PERL, Date & Time issues

Hello All, This is my first script in PERL. Hence require your help in moving further. I have a script which should populate the values for Today, Yesterday output. For which I use timeFrame as a variable to obtain the time in hrs:mm as 10:00. All I want is, I want my timeFrame to start... (4 Replies)
Discussion started by: sathyaonnuix
4 Replies

6. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

7. Shell Programming and Scripting

date formatting in perl

my code: $dateformat = yyyy_mm_dd if($dateformat =~ m/yyyy/i) { print ("found"); $yyyy = strftime "%Y", localtime; print ("year is $yyyy\n"); $dateformat =~ s/yyyy/$yyyy/; print ("new date format is $dateformat\n"); } elsif($dateformat =~ m/yy/i) { print ("found"); $yy = strftime "%y",... (4 Replies)
Discussion started by: irudayaraj
4 Replies

8. Shell Programming and Scripting

Creating a date (without time) in perl

I have a perl script that automatically runs on Mondays. I need to have it create a variable for last Monday's date thru that Sunday's date. example: 04-01-2011 thru 04-08-2011 Its reporting numbers for the previous week beginning with Monday and ending on Sunday. So i dont have to go in... (7 Replies)
Discussion started by: bbraml
7 Replies

9. Shell Programming and Scripting

Formatting date time in unix

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of the... (0 Replies)
Discussion started by: HeadBang
0 Replies

10. Shell Programming and Scripting

Date and Time in PERL

Hi, I want to get the current date and time and subtract 1 day to get to the previous day? I see timelocal( ) and (time) etc. How do I code this in PERL to get the previous day? Thanks Nurani (2 Replies)
Discussion started by: nurani
2 Replies
Login or Register to Ask a Question