johny has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm executing a internal tool command using system function in my script and the o/p of command is accessedpage and date
what i need is to get is accessed page and max of date ie jxbx__XYZ01 2007/10/10 as per below example. Do I need to store this o/p in afile and extract from it or can i mani +pulate the data and get the desired o/p output of the command executed using system jxbx__XYZ0099 2007/02/01 jxbx_XYZ098 2007/08/20 jxbx__XYZ01 2007/10/10 jxbx_XYZ0011 2007/08/05 jxbx_XYZ 2007/09/11 my $file = "pp.txt" ; open (FH ,$file) || die ("could not openfile"); while <FH>) { $usr = system ("listusr | grep $_ | cut -d ' ' -f1,3");
Regards Johny

Replies are listed 'Best First'.
Re: o/p of system
by Corion (Patriarch) on Oct 15, 2007 at 13:58 UTC

    As you've been told in the CB already, split is the answer for your questions, on many levels.

    1. Replace grep with the builtin grep.
    2. Replace cut with split.
    3. Split up the date on the slash ("/") and join it together again in YYYYMMDD form.
    4. Sort it according to the date
      split up the date on the slash ("/") and join it together again in YYYYMMDD form.

      I'm puzzled as to why you need to do that. Surely you could just sort it as it is without taking out the slashes. The OP still has the slashes in the desired output and a lexical sort will cope with the data as there are leading zeros for single-digit values. I missed the exchange in the CB but was the requirement to remove slashes mentioned there?

      Cheers,

      JohnGG

        That's an error on my part. Whenever I see dates delimited with slashes, I get the shivers because I can never be sure whether the date is in DD/MM/YYYY or MM/DD/YYYY form. Of course, here in this post, the date already was in a directly sortable order so the split/join was superfluous.

      Tnx Corion will try grep and split. Regards Johny
Re: o/p of system
by moritz (Cardinal) on Oct 15, 2007 at 13:55 UTC
    You need either backticks or the qx operator instead of the system command.
Re: o/p of system
by blue_cowdawg (Monsignor) on Oct 15, 2007 at 14:37 UTC
        $usr = system ("listusr | grep $_ | cut -d ' ' -f1,3");

    Dear Monk,
    Allow me to elaborate on what Corion has told you in the CB as well as here. Taking that line of code above I'd break it out as:

    # $usr = system ("listusr | grep $_ | cut -d ' ' -f1,3"); # becomes: my @usr = map { (split(/[\t\n\s]+/,$_))[0,2] } grep /$_/,qx(listusr);
    The problem with using system is, if you look at the documentation for the function call, is it does not return the IO stream of the execution like you think it might but it just executes the command and sends any output to STDOUT which you are not capturing in any way by using system. Using qx captures STDOUT and returns it as an array. See perlop for more info...

    UPDATE: simplified the <code>map{ split(...)} stuff at Corion's suggestion. (Thanks Corion!)


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg