LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2006, 09:58 AM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
Capture partial output from `date` command


I am writing a basic Perl script and I want to capture the day of the week from the `date` command and assign it to a variable.

The `date` command results in the following output:

Wed Dec 27 08:51:22 CST 2006

What is the best way to parse this output to capture the day of the week?
 
Old 12-27-2006, 10:27 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Check man date for options...

filename=`date '+%A'`
echo $filename
Wednesday
 
Old 12-27-2006, 10:37 AM   #3
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
'split' is your friend.

($weekday, $month, $day, $time, $zone, $year) = split(/ /, $DATE);

not sure if you need a syscall or not for your $DATE variable.
 
Old 12-27-2006, 11:38 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Within PERL (no system call - recommended):
Code:
#!/bin/perl
$daynumber = (localtime)[6];
($dayname = localtime) =~ s/\s.*//;
print "DAY_NUMBER = $daynumber, DAY_NAME = $dayname\n";
Or using a system call:
Code:
#!/bin/perl
($dayname = `date`) =~ s/\s.*//s;
print "DAY_NAME = $dayname\n";
 
Old 12-27-2006, 01:17 PM   #5
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by homey
Check man date for options...

filename=`date '+%A'`
echo $filename
Wednesday



When I capture the results of the date command, It seems that the results are not saved as a string. When I execute the code, I do not get any results:

(Today is Wednesday by the way)
[bclanton@BriansFC ~]$ date
Wed Dec 27 12:12:13 CST 2006



#!/usr/bin/perl
use Switch;


my $day = `date +%a`;
#my $day = Wed;
my $tape_label;



switch ($day) {
case ('Mon') {$tape_label = 'ISD001';}
case ('Tue') {$tape_label = 'ISD002';}
# case ('Wed') {$tape_label = 'ISD003'; print "$tape_label\n";}
case ('Wed') {print "Wednesday is working\n";}
case ('Thur') {$tape_label = 'ISD004';}
case ('Fri') {$tape_label = 'ISD005';}
case ('Sat') {$tape_label = 'ISD006';}
case ('Sun') {$tape_label = 'ISD007';}
}

[bclanton@BriansFC ~]$ ./perltest.pl
[bclanton@BriansFC ~]$


Now, when I explicitly assign my $day variable to 'Wed', then I get the correct results.

#!/usr/bin/perl
use Switch;


#my $day = `date +%a`;
my $day = Wed;
my $tape_label;



switch ($day) {
case ('Mon') {$tape_label = 'ISD001';}
case ('Tue') {$tape_label = 'ISD002';}
# case ('Wed') {$tape_label = 'ISD003'; print "$tape_label\n";}
case ('Wed') {print "Wednesday is working\n";}
case ('Thur') {$tape_label = 'ISD004';}
case ('Fri') {$tape_label = 'ISD005';}
case ('Sat') {$tape_label = 'ISD006';}
case ('Sun') {$tape_label = 'ISD007';}
}

[bclanton@BriansFC ~]$ ./perltest.pl
Wednesday is working


Do I need to format my day variable as a string or am I using the 'date' function incorrectly?

Also, what is the C default statement for case statements in Perl? For instance, if for some reason my case variable doesn't match anything in the block, what syntax does Perl use to default out and break out for the case block?

Last edited by tekmann33; 12-27-2006 at 01:26 PM.
 
Old 12-27-2006, 01:40 PM   #6
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 23, 24, RHL 5.2
Posts: 560

Rep: Reputation: 56
There are two problems in this one. The command `date +%a` is inserting a new line into $day. The second problem is in your case statements. () is used for grouping and not really applicable in this instance. You should use double quotes if you want an exact match not single quotes. Replacing ('Wed') with "Wed\n" will work. If I were doing this I would probably use /Wed/. That will match any occurance of Wed in $day.

Here is a good tutorial on Perl regular expressions. I also thought haertig had a good suggestion using the localtime function.

Bill

ps - Just wanted to add a posting hint. This probably would of been good to post in the programming forum. Also it makes reading code easier if you post it using the code feature of LQ. Go Advanced and use the # link.

Last edited by wmakowski; 12-27-2006 at 01:46 PM.
 
Old 12-27-2006, 01:43 PM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Is your goal to learn the switch command, or just to get an appropriate $tape_label variable set? The following is easier and doesn't require you to use the Switch module:
Code:
$daynum = (localtime)[6];
$daynum = 7 if $daynum <= 0;
$tape_label = 'ISD00' . $daynum;
[edit] Corrected $daynum calculation since you wanted Monday to be '1' and Sunday to be '7'. (Previously I had it with Sunday being '1' and Saturday being '7'.) [/edit]

Last edited by haertig; 12-27-2006 at 01:49 PM.
 
Old 12-27-2006, 02:09 PM   #8
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by tekmann33
Also, what is the C default statement for case statements in Perl?
There is no case statement in PERL. You found the Switch module, but that's not an operator/function that is native to PERL. Below is the closest I can think of to a "native" PERL case statement, including a default value if nothing matches:
Code:
#!/usr/bin/perl
$_ = `date`;
{
    /Mon/       && ($tape_label = 'ISD001', last);
    /Tue/       && ($tape_label = 'ISD002', last);
    /Wed/       && ($tape_label = 'ISD003', last);
    /Thu/       && ($tape_label = 'ISD004', last);
    /Fri/       && ($tape_label = 'ISD005', last);
    /Sat/       && ($tape_label = 'ISD006', last);
    /Sun/       && ($tape_label = 'ISD007', last);
    $tape_label  = 'this_is_the_unmatched_default_value';
}
print "$tape_label\n";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
capture the output to a variable Melsync Linux - General 4 09-07-2006 03:17 PM
can't capture 'at' command standard output to a file bartoni Linux - General 3 02-28-2006 04:52 PM
Partial NTSC fix for Video Capture based on SAA7130/34 Electro Linux - General 3 09-18-2005 02:57 AM
PHP - incremental/partial output of a page ? michaelsanford Programming 1 07-13-2005 01:37 AM
capture make's output gboutwel Linux - Newbie 2 07-07-2002 06:15 AM


All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration