473,804 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 17 '05 #1
14 2185

"deko" <no****@hotmail .com> wrote in message
news:TD******** ***********@new ssvr29.news.pro digy.com...
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.


$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
Jul 17 '05 #2
In article <TD************ *******@newssvr 29.news.prodigy .com>, deko wrote:
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?


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>
Jul 17 '05 #3
> $f=file("filena me.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?
Jul 17 '05 #4
> 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;
I think I'm having trouble with #4

// $visdata = 'visdata.txt' 68.122.69.241|1 089822686
68.122.69.241|1 089823630
68.122.69.241|1 089823638
68.122.69.241|1 089828547


$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?
Jul 17 '05 #5
> 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;


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)) ??
Jul 17 '05 #6
"deko" <no****@hotmail .com> wrote in message
news:ud******** ***********@new ssvr29.news.pro digy.com...
$f=file("filena me.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?


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 :-)
Jul 17 '05 #7
deko wrote:
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;

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)) ??


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
:)
Jul 17 '05 #8
Chung Leong wrote:
"deko" <no****@hotmail .com> wrote in message
news:ud******** ***********@new ssvr29.news.pro digy.com...
$f=file("fil ename.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
troubleshoo t? How can I be sure I even have an array?

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 :-)


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
:)
Jul 17 '05 #9
> 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
:)


Thanks, Mike. I just re-posted the question in a more clear way. I changed
the way I created the file, which should help.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
61422
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: in_file = raw_input("What is the name of the file you want to open: ") in_file = open("test.txt","r")
5
44345
by: Mike Bannon | last post by:
Hi All We have an order processing database which includes the standard Order Header/Order Lines tables. I'm trying to write a query to get the average number of lines per order over a given period, and I'm stuck :-( I can get the number of lines per order with: SELECT COUNT(*) FROM OrderDetails INNER JOIN Order Header ON
1
3198
by: James | last post by:
Access 2003, trying to count the number of records that meet a criteria. According to Help: "In the Database window, click Queries under Objects, and then click New on the database window toolbar. Add the table, view, or function you want to summarize in the Diagram pane. " OK, that's easy.....but then..... "Right-click the background of the Diagram pane, then choose Group By
68
6838
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
1
2210
by: Starke | last post by:
I have a table where Im counting records that have a two certain criteria. My Question is, If one criteria is met, but the other isnt instead of not retrieving the record can the count value return "0"? Ie My query is as below. Employee ID --->no criteria to meet Shift = B Pass = True (its a check box)
1
1899
by: Bhujanga | last post by:
I have some reports whose purpose is to show whether any records currently meet certain criteria, so of course the report is based on a query where that criteria is defined. If there don't happen to be any records that satisfy the criteria at a particular time then the report produces with one line in the detail section that says "#Error". This confuses the users becuase they think there is something wrong with the report. Is there a way to...
5
10871
by: Soccer5 | last post by:
Trying to Count records on a report that meet a certain criteria. Have a text box in the Report Footer that has the following in the Control Source: =Count(="S") This does not work. It counts ALL records whether Record Type = "S" or "M" There is nothing unique about the records to distinguish S-type records from the M-type records.
17
2971
by: swuwarrior | last post by:
Whats up guys. I just decided i would try porgramming as i am a physical education major. As apart of my major i deal with alot of paper work. So i wanted to write a program to mess with all the papers i have to deal with. My question is i have read in my text file and can display the WHOLE text file. Now i want to be able to look at individual lines of my text file to see if they meet a certain criteria. can anyone help me with this...
3
2715
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word. #include <fstream> #include <iostream> #include <string> #include <cstdlib> using namespace std;
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.