How to count lines in a file that meet criteria?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    How to count lines in a file that meet criteria?

    Below are the contents file that has the IP address and time of visit of
    visitors to a website.



    68.122.69.241|1 089822686

    68.122.69.241|1 089823630

    68.122.69.241|1 089823638

    68.122.69.241|1 089828547

    The second column is the output of time()

    I need a script to count each line in this file where the time stamp is
    greater than time() - 3600*24. This will give number of visitors in the
    last 24 hours. I also need number of visitors in the last 30 days, and
    year. Should I use an array? Suggestions for getting started?

    Thanks in advance.


  • Garp

    #2
    Re: How to count lines in a file that meet criteria?


    "deko" <nospam@hotmail .com> wrote in message
    news:TDgJc.9190 6$Ql1.76743@new ssvr29.news.pro digy.com...[color=blue]
    > Below are the contents file that has the IP address and time of visit of
    > visitors to a website.
    >
    >
    >
    > 68.122.69.241|1 089822686
    >
    > 68.122.69.241|1 089823630
    >
    > 68.122.69.241|1 089823638
    >
    > 68.122.69.241|1 089828547
    >
    > The second column is the output of time()
    >
    > I need a script to count each line in this file where the time stamp is
    > greater than time() - 3600*24. This will give number of visitors in the
    > last 24 hours. I also need number of visitors in the last 30 days, and
    > year. Should I use an array? Suggestions for getting started?
    >
    > Thanks in advance.
    >
    >[/color]

    $f=file("filena me.txt");
    foreach($f as $line) {
    $parts=explode( "|",$line);
    if($parts[1] > time()-(3600*24)) { ++$visitors; }
    }

    Just a suggestion, see how far you get (untested, no warranty, etc).

    Garp


    Comment

    • Tim Van Wassenhove

      #3
      Re: How to count lines in a file that meet criteria?

      In article <TDgJc.91906$Ql 1.76743@newssvr 29.news.prodigy .com>, deko wrote:[color=blue]
      > Below are the contents file that has the IP address and time of visit of
      > visitors to a website.
      >
      >
      >
      > 68.122.69.241|1 089822686
      >
      > 68.122.69.241|1 089823630
      >
      > 68.122.69.241|1 089823638
      >
      > 68.122.69.241|1 089828547
      >
      > The second column is the output of time()
      >
      > I need a script to count each line in this file where the time stamp is
      > greater than time() - 3600*24. This will give number of visitors in the
      > last 24 hours. I also need number of visitors in the last 30 days, and
      > year. Should I use an array? Suggestions for getting started?[/color]

      1) determine $time;
      2) $count = 0;
      3) open file;
      4) read file line by line
      split/explode line based on | into $ip and $time2
      if $time2 > $time -> count = count + 1;
      5) close file;
      6) return count;

      The sections on strings and file handling in the manual
      at http://www.php.net/manual will be more than enough

      --
      Tim Van Wassenhove <http://home.mysth.be/~timvw>

      Comment

      • deko

        #4
        Re: How to count lines in a file that meet criteria?

        > $f=file("filena me.txt");[color=blue]
        > foreach($f as $line) {
        > $parts=explode( "|",$line);
        > if($parts[1] > time()-(3600*24)) { ++$visitors; }
        > }[/color]

        $f = file($visdata);
        foreach($f as $line)
        {
        $p = explode("|",$li ne);
        if($p[1]>time()-(3600*24))
        {
        ++$f24h;
        }
        }
        echo "<br>f24=".$f24 h

        this returns a blank for f24 - is there anyway to walk thrugh this to
        troubleshoot? How can I be sure I even have an array?


        Comment

        • deko

          #5
          Re: How to count lines in a file that meet criteria?

          > 1) determine $time;[color=blue]
          > 2) $count = 0;
          > 3) open file;
          > 4) read file line by line
          > split/explode line based on | into $ip and $time2
          > if $time2 > $time -> count = count + 1;
          > 5) close file;
          > 6) return count;[/color]

          I think I'm having trouble with #4

          // $visdata = 'visdata.txt'[color=blue]
          > 68.122.69.241|1 089822686
          > 68.122.69.241|1 089823630
          > 68.122.69.241|1 089823638
          > 68.122.69.241|1 089828547[/color]

          $av = file($visdata);
          $v24h = 0;
          foreach($av as $line)
          {
          $pv = explode("|",$li ne);
          if($pv[1]<time()-(3600*24))
          {
          ++$v24h;
          }
          }
          echo "<br>v24h=".$v2 4h;

          This is not working... is this the best way to parse the array line by line?


          Comment

          • deko

            #6
            Re: How to count lines in a file that meet criteria?

            > 1) determine $time;[color=blue]
            > 2) $count = 0;
            > 3) open file;
            > 4) read file line by line
            > split/explode line based on | into $ip and $time2
            > if $time2 > $time -> count = count + 1;
            > 5) close file;
            > 6) return count;[/color]

            Okay. I got this working:

            $av = file($visdata);
            $v24h = 0;
            foreach($av as $line)
            {
            $pv = explode("|",$li ne);
            //if($pv[5]>time())-(3600*24))
            // {
            ++$v24h;
            // }
            }
            echo "<br>v24h=".$v2 4h; //returns number of lines in the file.

            Now if I can just find the best way to inspect the timestamp ... perhaps I
            need to use something like:

            if number_format($ pv[5])>(time()-(3600*24)) ??


            Comment

            • Chung Leong

              #7
              Re: How to count lines in a file that meet criteria?

              "deko" <nospam@hotmail .com> wrote in message
              news:udiJc.9196 1$NZ1.48463@new ssvr29.news.pro digy.com...[color=blue][color=green]
              > > $f=file("filena me.txt");
              > > foreach($f as $line) {
              > > $parts=explode( "|",$line);
              > > if($parts[1] > time()-(3600*24)) { ++$visitors; }
              > > }[/color]
              >
              > $f = file($visdata);
              > foreach($f as $line)
              > {
              > $p = explode("|",$li ne);
              > if($p[1]>time()-(3600*24))
              > {
              > ++$f24h;
              > }
              > }
              > echo "<br>f24=".$f24 h
              >
              > this returns a blank for f24 - is there anyway to walk thrugh this to
              > troubleshoot? How can I be sure I even have an array?[/color]

              You didn't set $f24h to zero prior to incrementing it, and ++ does not cast
              a null or a boolean into an int. Thus $f24h remains null :-)


              Comment

              • Michael Austin

                #8
                Re: How to count lines in a file that meet criteria?

                deko wrote:[color=blue][color=green]
                >>1) determine $time;
                >>2) $count = 0;
                >>3) open file;
                >>4) read file line by line
                >> split/explode line based on | into $ip and $time2
                >> if $time2 > $time -> count = count + 1;
                >>5) close file;
                >>6) return count;[/color]
                >
                >
                > Okay. I got this working:
                >
                > $av = file($visdata);
                > $v24h = 0;
                > foreach($av as $line)
                > {
                > $pv = explode("|",$li ne);
                > //if($pv[5]>time())-(3600*24))
                > // {
                > ++$v24h;
                > // }
                > }
                > echo "<br>v24h=".$v2 4h; //returns number of lines in the file.
                >
                > Now if I can just find the best way to inspect the timestamp ... perhaps I
                > need to use something like:
                >
                > if number_format($ pv[5])>(time()-(3600*24)) ??
                >
                >[/color]

                you put it an array of arrays so:

                $av = file($visdata);
                $v24h = 0;
                foreach($av as $line)
                {
                $pv = explode("|",$li ne); // put each line into an array
                foreach ($pv as $key[5]=>$value){ // or whatever the key is....
                if($value > time()-(3600*24)) // now check the time
                {
                ++$v24h;
                }
                }
                }
                echo "<br>v24h=".$v2 4h; //returns number of lines in the file.

                --
                Michael Austin.
                Consultant - Available.
                Donations welcomed. Http://www.firstdbasource.com/donations.html
                :)

                Comment

                • Michael Austin

                  #9
                  Re: How to count lines in a file that meet criteria?

                  Chung Leong wrote:
                  [color=blue]
                  > "deko" <nospam@hotmail .com> wrote in message
                  > news:udiJc.9196 1$NZ1.48463@new ssvr29.news.pro digy.com...
                  >[color=green][color=darkred]
                  >>>$f=file("fil ename.txt");
                  >>>foreach($f as $line) {
                  >>> $parts=explode( "|",$line);
                  >>> if($parts[1] > time()-(3600*24)) { ++$visitors; }
                  >>>}[/color]
                  >>
                  >>$f = file($visdata);
                  >>foreach($f as $line)
                  >> {
                  >> $p = explode("|",$li ne);
                  >> if($p[1]>time()-(3600*24))
                  >> {
                  >> ++$f24h;
                  >> }
                  >> }
                  >>echo "<br>f24=".$f24 h
                  >>
                  >>this returns a blank for f24 - is there anyway to walk thrugh this to
                  >>troubleshoo t? How can I be sure I even have an array?[/color]
                  >
                  >
                  > You didn't set $f24h to zero prior to incrementing it, and ++ does not cast
                  > a null or a boolean into an int. Thus $f24h remains null :-)
                  >[/color]

                  he had a problem with how he was trying to handle his arrays...

                  $i = 0;
                  for ($i=0;$i<10;$i+ +)
                  { $x++;
                  ++$y;
                  }
                  echo $x ."\n";
                  echo $y ."\n";

                  the variables do not "pre-exist" and are incremented correctly.

                  --
                  Michael Austin.
                  Consultant - Available.
                  Donations welcomed. Http://www.firstdbasource.com/donations.html
                  :)

                  Comment

                  • deko

                    #10
                    Re: How to count lines in a file that meet criteria?

                    > he had a problem with how he was trying to handle his arrays...[color=blue]
                    >
                    > $i = 0;
                    > for ($i=0;$i<10;$i+ +)
                    > { $x++;
                    > ++$y;
                    > }
                    > echo $x ."\n";
                    > echo $y ."\n";
                    >
                    > the variables do not "pre-exist" and are incremented correctly.
                    >
                    > --
                    > Michael Austin.
                    > Consultant - Available.
                    > Donations welcomed. Http://www.firstdbasource.com/donations.html
                    > :)[/color]

                    Thanks, Mike. I just re-posted the question in a more clear way. I changed
                    the way I created the file, which should help.


                    Comment

                    • Tim Van Wassenhove

                      #11
                      Re: How to count lines in a file that meet criteria?

                      In article <lGiJc.91966$qa 2.23534@newssvr 29.news.prodigy .com>, deko wrote:[color=blue][color=green]
                      >> 1) determine $time;
                      >> 2) $count = 0;
                      >> 3) open file;
                      >> 4) read file line by line
                      >> split/explode line based on | into $ip and $time2
                      >> if $time2 > $time -> count = count + 1;
                      >> 5) close file;
                      >> 6) return count;[/color][/color]
                      [color=blue]
                      > I think I'm having trouble with #4
                      > $av = file($visdata);[/color]
                      [color=blue]
                      > $v24h = 0;
                      > foreach($av as $line)
                      > {
                      > $pv = explode("|",$li ne);
                      > if($pv[1]<time()-(3600*24))
                      > {
                      > ++$v24h;
                      > }
                      > }
                      > echo "<br>v24h=".$v2 4h;
                      >
                      > This is not working... is this the best way to parse the array line by line?[/color]

                      As you are only parsing it line by line, i don't see why you would load
                      the whole file in memory first...

                      --
                      Tim Van Wassenhove <http://home.mysth.be/~timvw>

                      Comment

                      • lawrence

                        #12
                        Re: How to count lines in a file that meet criteria?

                        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<JqOdnRo_f cGMUGjd4p2dnA@c omcast.com>...[color=blue]
                        > "deko" <nospam@hotmail .com> wrote in message
                        > news:udiJc.9196 1$NZ1.48463@new ssvr29.news.pro digy.com...[color=green][color=darkred]
                        > > > $f=file("filena me.txt");
                        > > > foreach($f as $line) {
                        > > > $parts=explode( "|",$line);
                        > > > if($parts[1] > time()-(3600*24)) { ++$visitors; }
                        > > > }[/color]
                        > >
                        > > $f = file($visdata);
                        > > foreach($f as $line)
                        > > {
                        > > $p = explode("|",$li ne);
                        > > if($p[1]>time()-(3600*24))
                        > > {
                        > > ++$f24h;
                        > > }
                        > > }
                        > > echo "<br>f24=".$f24 h
                        > >
                        > > this returns a blank for f24 - is there anyway to walk thrugh this to
                        > > troubleshoot? How can I be sure I even have an array?[/color]
                        >
                        > You didn't set $f24h to zero prior to incrementing it, and ++ does not cast
                        > a null or a boolean into an int. Thus $f24h remains null :-)[/color]

                        Speaking of casting, can you cast a null to an int just by doing this:


                        foreach($f as $line)
                        {
                        $p = explode("|",$li ne);
                        if($p[1]>time()-(3600*24))
                        {
                        (int)$f24h++;
                        }
                        }


                        Or is this the better way to do it:


                        foreach($f as $line)
                        {
                        $p = explode("|",$li ne);
                        if($p[1]>time()-(3600*24))
                        {
                        $f24h = (int)$f24h + 1;
                        }
                        }

                        Comment

                        • Michael Austin

                          #13
                          Re: How to count lines in a file that meet criteria?

                          lawrence wrote:[color=blue]
                          > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<JqOdnRo_f cGMUGjd4p2dnA@c omcast.com>...
                          >[color=green]
                          >>"deko" <nospam@hotmail .com> wrote in message
                          >>news:udiJc.91 961$NZ1.48463@n ewssvr29.news.p rodigy.com...
                          >>[color=darkred]
                          >>>>$f=file("fi lename.txt");
                          >>>>foreach($ f as $line) {
                          >>>> $parts=explode( "|",$line);
                          >>>> if($parts[1] > time()-(3600*24)) { ++$visitors; }
                          >>>>}
                          >>>
                          >>>$f = file($visdata);
                          >>>foreach($f as $line)
                          >>> {
                          >>> $p = explode("|",$li ne);
                          >>> if($p[1]>time()-(3600*24))
                          >>> {
                          >>> ++$f24h;
                          >>> }
                          >>> }
                          >>>echo "<br>f24=".$f24 h
                          >>>
                          >>>this returns a blank for f24 - is there anyway to walk thrugh this to
                          >>>troubleshoot ? How can I be sure I even have an array?[/color]
                          >>
                          >>You didn't set $f24h to zero prior to incrementing it, and ++ does not cast
                          >>a null or a boolean into an int. Thus $f24h remains null :-)[/color]
                          >
                          >
                          > Speaking of casting, can you cast a null to an int just by doing this:
                          >
                          >
                          > foreach($f as $line)
                          > {
                          > $p = explode("|",$li ne);
                          > if($p[1]>time()-(3600*24))
                          > {
                          > (int)$f24h++;
                          > }
                          > }[/color]

                          No. because you only get to that line when the "if" statement is true.

                          So, if it is not initialized AND you want it to display 0(zero) if no values
                          found then you must cast it to int before displaying it. IMHO, it is easier to
                          just init the variable.

                          if (!isset($v24h)) {$v24h=(int)$v2 4h;}
                          echo......
                          --
                          Michael Austin.
                          Consultant - Available.
                          Donations welcomed. Http://www.firstdbasource.com/donations.html
                          :)

                          Comment

                          • deko

                            #14
                            Re: How to count lines in a file that meet criteria?

                            > As you are only parsing it line by line, i don't see why you would load[color=blue]
                            > the whole file in memory first...[/color]

                            Is it resource intensive to read a file into memory with file($myfile)? How
                            big of a file is too big? What do you think the performance penalty is for
                            a 500K file? How about a 2Mg file? Are there any benchmarks? Isn't the
                            script faster when workig with an array, since it's all in memory?


                            Comment

                            • Chung Leong

                              #15
                              Re: How to count lines in a file that meet criteria?

                              "Michael Austin" <maustin@firstd basource.com> wrote in message
                              news:tukJc.2315 $Wj3.1398@newss vr22.news.prodi gy.com...
                              [color=blue]
                              > he had a problem with how he was trying to handle his arrays...
                              >
                              > $i = 0;
                              > for ($i=0;$i<10;$i+ +)
                              > { $x++;
                              > ++$y;
                              > }
                              > echo $x ."\n";
                              > echo $y ."\n";
                              >
                              > the variables do not "pre-exist" and are incremented correctly.
                              >
                              > --
                              > Michael Austin.
                              > Consultant - Available.
                              > Donations welcomed. Http://www.firstdbasource.com/donations.html
                              > :)[/color]

                              Shoot. Should have tested it first before posting :-p


                              Comment

                              Working...