Isolating the Month in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Isolating the Month in Perl
# 1  
Old 02-06-2009
Isolating the Month in Perl

munt=`date '+%m` will isolate the month in digit form 02 = Feb

Trying to get the same out of perl just cant see it


$stimx = localtime($^T);
print ((split/ /,$stimx)[1]);
# 2  
Old 02-06-2009
just need a newline, i think:

Code:

$stimx = localtime($^T);


print ((split/ /,$stimx)[1]);
print "\n";

# 3  
Old 02-06-2009
oh, in digit form:

Code:
#----------------------------------------------------------------------#
# perl localtime.                                                      #
#----------------------------------------------------------------------#
$now = time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  localtime( $now );

#----------------------------------------------------------------------#
# convert to datetime.                                                 #
#----------------------------------------------------------------------#
$current = sprintf( "%04d-%02d-%02d %02d:%02d:%02d",
  $year + 1900,
  $mon + 1,
  $mday,
  $hour,
  $min,
  $sec
  );

print "current-date: $current\n";

month only:

Code:
#----------------------------------------------------------------------#
# convert to datetime.                                                 #
#----------------------------------------------------------------------#
$current_month = sprintf( "%02d", $mon + 1 );
 
print "current-month: $current_month\n";

# 4  
Old 02-06-2009
Code:
zsh-4.3.9[sysadmin]% perl -e'printf "%.2d\n",(localtime)[4]+1'
02

# 5  
Old 02-06-2009
Quote:
Originally Posted by popeye
munt=`date '+%m` will isolate the month in digit form 02 = Feb

Trying to get the same out of perl just cant see it


$stimx = localtime($^T);
print ((split/ /,$stimx)[1]);
If you had read the localtime() man page you would see that in scalar context localtime returns a different value than it does when used in list context. You are getting the name of the month instead of the number of the month because you are using localtime in scalar context.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Isolating a chunk of text using php

greetings, i'll start by stating; i am NOT looking for the EXACT syntax to my query but a simple yes or no of its possibility. and if you're feeling generous maybe the php function(s) that i'd use as a jump start. i could use bash but i really want to take a shot at doing this with php. the... (0 Replies)
Discussion started by: crimso
0 Replies

2. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies

3. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

4. Shell Programming and Scripting

[Solved] Isolating & Counting IP from log file

Dear Community, today my website was under attack for several hours. 2 specific IPs make a tons of "get requests" to a specific page and apache server goes up and down. Now the problem is solved because I put in firewall blacklist these IPs, but I took a lot of time to analyze the apache log to... (6 Replies)
Discussion started by: Lord Spectre
6 Replies

5. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

6. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

7. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

8. UNIX for Dummies Questions & Answers

Isolating Stat Results

i'm trying to isolate the results from the stat command to just the file name and the size. I got as far as: stat *.jpg | grep Size How can I isolate the size and the file name? (3 Replies)
Discussion started by: jvpike
3 Replies

9. Shell Programming and Scripting

Isolating a specified line - awk grep or somthing else?

I'm trying to isolate attached hard drives that auto-mount to /media so that I can use them as variables in a bash script... so far I'm here: variable=$(ls /media | grep -v cdrom ) This lists all the connected drives, each on it's own line and doesn't list anything I don't want (cdrom... (2 Replies)
Discussion started by: Starcast
2 Replies

10. Shell Programming and Scripting

Perl Script : Split given month into weeks

I want to split a given month into weeks. For example if I give the date in dd/mm/yy format say 01/02/08 it should give output in the given format : week1 : start date and end date. week2 : "" week3 : "" week4 : "" (5 Replies)
Discussion started by: khushbu_roy
5 Replies
Login or Register to Ask a Question