formatting datetime in perl

November 9, 2010 at 07:56:16
Specs: Unix
while read l
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?

See More: formatting datetime in perl

Report •

November 9, 2010 at 09:20:36
I guess it could be a memory or disk space problem because your program is very inefficient. You are calling the perl script for each row in the file. Is that really necessary? How about setting a date time variable just once outside the while loop?

If I am interpreting what you are trying to do:

1) read each line into a variable
2) place the time from the perl script in front of each line and place it in another var
3) print to a file: the line variable, 2 pipe symbols, and the var determined in 2)

Your output file will be over twice as large as your input file. Is that what you want?

Report •

November 9, 2010 at 18:50:22
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

Report •

November 10, 2010 at 22:24:53
Wow! Of course, how stupid of me! You could parse the output of the ls command to get the same information, but I am not certain you would gain much by doing that. I think the problem might be the size of the file. It's hard to say.

You might consider splitting the file into smaller parts, perform your processing, and then concatenate the smaller files back together. The unix split or csplit commands can split the file into pieces.

Report •
Related Solutions

Ask Question