Loading ...
Sorry, an error occurred while loading the content.

Problem with SPLIT function...for "date" command in Linux

Expand Messages
  • shailesh
    hello, This is my perl script to split the various entities available as part of the date command in Linux: #start $date = `date`; print Today is $date n ;
    Message 1 of 3 , Oct 5, 2006
    • 0 Attachment
      hello,
      This is my perl script to split the various entities available as
      part of the "date" command in Linux:

      #start
      $date = `date`;
      print "Today is $date\n";
      ($day, $month, $dat, $time, $area, $year) = split(/ /,$date);
      print "Day : $day\n";
      print "Month : $month\n";
      print "Date : $dat\n";
      print "Time : $time\n";
      print "Area : $area\n";
      print "Year : $year\n";
      #end

      This is the output of running the above :
      #<<start of output>>
      Today is Thu Oct 5 09:44:07 IST 2006

      Day : Thu
      Month : Oct
      Date :
      Time : 5
      Area : 09:44:07
      Year : IST
      #<<end of output>>

      My doubt: Why is the Date output empty ?? Is it becoz of the double
      space b/w "Oct" and "5".. the time, area and year are wrong also..!!!

      I believe the split shd work irrespective of the no. of spaces in
      b/w the character strings..

      Plz let me know
      shailesh.
    • Damien Carbery
      ... The simplest fix is to change your split call to: split(/ s+/,$date); This will allow for more than one space to be between items. A more portable solution
      Message 2 of 3 , Oct 5, 2006
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "shailesh" <shail_hegde@...> wrote:
        >
        > hello,
        > This is my perl script to split the various entities available as
        > part of the "date" command in Linux:
        >
        > #start
        > $date = `date`;
        > print "Today is $date\n";
        > ($day, $month, $dat, $time, $area, $year) = split(/ /,$date);
        > print "Day : $day\n";
        > print "Month : $month\n";
        > print "Date : $dat\n";
        > print "Time : $time\n";
        > print "Area : $area\n";
        > print "Year : $year\n";
        > #end
        >
        > This is the output of running the above :
        > #<<start of output>>
        > Today is Thu Oct 5 09:44:07 IST 2006
        >
        > Day : Thu
        > Month : Oct
        > Date :
        > Time : 5
        > Area : 09:44:07
        > Year : IST
        > #<<end of output>>
        >
        > My doubt: Why is the Date output empty ?? Is it becoz of the double
        > space b/w "Oct" and "5".. the time, area and year are wrong also..!!!
        >
        > I believe the split shd work irrespective of the no. of spaces in
        > b/w the character strings..
        >
        > Plz let me know
        > shailesh.
        >
        The simplest fix is to change your split call to:
        split(/\s+/,$date);
        This will allow for more than one space to be between items.

        A more portable solution is to not call the external 'date' command
        but use localtime/gmtime and/or strftime.

        use POSIX; # To get the strftime function.
        my ($day, $month, $dat, $time, $area, $year) = split( /\s+/, strftime(
        '%a %b %d %T %Z %Y',localtime) );
        print "Day : $day\n";
        print "Month : $month\n";
        print "Date : $dat\n";
        print "Time : $time\n";
        print "Area : $area\n";
        print "Year : $year\n";

        # Also look at the localtime function on its own:
        my @elements = localtime;
        print join( ', ', @elements );
        # perldoc -f localtime for more information.
        # gmtime function is similar but returns time for GMT in London.
      • gopi ramya
        Hi Shailesh, Yes ur rigth. The space BW the Month and Day normally took two white space . Regarding the The month Value comes in the shorter form. For that
        Message 3 of 3 , Oct 5, 2006
        • 0 Attachment
          Hi Shailesh,
          Yes ur rigth. The space BW the Month and Day normally
          took two white space . Regarding the The month Value
          comes in the shorter form. For that particular reason
          only we need to do the same as below.

          # Declaing the Variables
          my($r_output,$r_time,$am_pm,$r_hour,$r_min,$r_sec,$r_date,$r_month,$r_year,$status_report_time);

          # Get the Date value from the Date function
          $r_output=`date +%r,%d,%B,%Y`;

          #Split the $r_output and do the Manipulation

          ($r_time,$r_date,$r_month,$r_year)=split(/,/,$r_output);
          ($time,$am_pm)=split(/ /,$r_time);
          ($r_hour,$r_min,$r_sec)=split(/:/,$time);
          # The $status_report_time is the final output.
          $status_report_time=$r_hour.":".$r_min." ".$am_pm."
          local time, ".$r_month." ".$r_date.", ".$r_year;

          --- shailesh <shail_hegde@...> wrote:

          > hello,
          > This is my perl script to split the various entities
          > available as
          > part of the "date" command in Linux:
          >
          > #start
          > $date = `date`;
          > print "Today is $date\n";
          > ($day, $month, $dat, $time, $area, $year) = split(/
          > /,$date);
          > print "Day : $day\n";
          > print "Month : $month\n";
          > print "Date : $dat\n";
          > print "Time : $time\n";
          > print "Area : $area\n";
          > print "Year : $year\n";
          > #end
          >
          > This is the output of running the above :
          > #<<start of output>>
          > Today is Thu Oct 5 09:44:07 IST 2006
          >
          > Day : Thu
          > Month : Oct
          > Date :
          > Time : 5
          > Area : 09:44:07
          > Year : IST
          > #<<end of output>>
          >
          > My doubt: Why is the Date output empty ?? Is it
          > becoz of the double
          > space b/w "Oct" and "5".. the time, area and year
          > are wrong also..!!!
          >
          > I believe the split shd work irrespective of the no.
          > of spaces in
          > b/w the character strings..
          >
          > Plz let me know
          > shailesh.
          >
          >
          >
          >
          >


          Thanks && Regrads
          GOPI . R
          94435 92202

          __________________________________________________
          Do You Yahoo!?
          Tired of spam? Yahoo! Mail has the best spam protection around
          http://mail.yahoo.com
        Your message has been successfully submitted and would be delivered to recipients shortly.