LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2012, 04:55 PM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
What kind of search routine do I need?


If I have data writen to a file on my server that takes this flavor:
Code:
12/25/2012, Tx, Houston, SingersName, SingersWebSite, VenueWebSite
2/20/2013, Ar, Little Rock, SingersName, SingersWebSite, VenueWebSite
10/15/2012, Ok Tulsa, SingersName, SingersWebSite, VenueWebSite
2/25/2012, Tx, Dallas, SingersName, SingersWebSite, VenueWebSite
6/25/2012, Or, Portland, SingersName, SingersWebSite, VenueWebSite
And I want to search to see if an event is happening, will it be as simple as:
Code:
readline
	if element(1) == request(1) and
			if element(2) == request(2) and
				if element(3) == request(3) then
		print all 6 elements to screen
	else
		read next line
I am more interested in what kind of search this would be. Seems PHP more that enough to take care of my needs. Especially on 'PHP.net'.
 
Old 12-24-2012, 08:37 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Practically, I am not sure whether ultimate end users would key in exact and full dates (they might want a range) or city names. An option would be to have the user select the date (or date range) on line, let the user choose form the drop down list of states (static) and then based on it, cities with open performances.

Secondly, it is generally believed (though I have no proof) that using search tools would be more eficient compared to custom built nested-ifs. I mean once you get the parameters, a sed, grep or awk should do.

OK
 
Old 12-24-2012, 10:09 PM   #3
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by AnanthaP View Post
Practically, I am not sure whether ultimate end users would key in exact and full dates (they might want a range) or city names. An option would be to have the user select the date (or date range) on line, let the user choose form the drop down list of states (static) and then based on it, cities with open performances.
That is exactly what will happen. I posted the code that wrote that data in another thread. Just need to finish it.

Quote:
Secondly, it is generally believed (though I have no proof) that using search tools would be more eficient compared to custom built nested-ifs. I mean once you get the parameters, a sed, grep or awk should do. OK
Interesting. This would make things so much simpler. Will 'grep' et al be available on linux based servers?

Thanks for this new insight.
Dave
 
Old 12-25-2012, 07:00 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
grep, sed, and awk are defined in the POSIX standard as core system tools, so yes, some version should be available on all *nix-based systems.

Almost all Linux distributions use the GNU versions, which have many useful extensions, but the posix syntax should be pretty much universally portable.
 
Old 12-26-2012, 02:20 PM   #5
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
I searched for an example using either 'system(grep)' or 'exec(grep)', found nothing. Did find some alternitive sugguestions. This is one of them.

This is the code adapted for an example of 'strpos' found here: http://www.ehow.com/how_8481508_stri...#ixzz2GBM2EfuW

Not working as expected.
Code:
<?php
	$file = fopen("WhereAmIPlaying", "r") or exit("Unable to open file!");
	$searchPattern = "01/31/2013, TX, Austin";

	while(!feof($file))
	  {
	 	 $stringToSearch = fgets($file);
				printf('%s', $stringToSearch); // test for data
				printf('%s', $searchPattern);  // test for data
    		$locationOfString = strpos($stringToSearch , $searchPattern );

			if($locationOfString == false) 
			{ 
				print('string not found'); 
			}
			else 
			{ 
				printf('%s', $stringToSearch); 
			}
	 }
?>
This is the content of 'WhereAmIPlaying'.
Quote:
01/04/2013, Tx, Austin, Lake Street Dive, LSD.com, WhipInn.net
01/16/2013, Tx, Huntsville, Becca Loebe, RebeccaLoebe.com, ArHouseConcerts.com
12/29/2012, Tx, Hem[steqe, Kendra Smith, KS.com, KS.net
02/08/2013, Al, Atlanta, Becca Loebe, RebeccaLoebe.com, EddoesAtoc/cp,
01/31/2013, Tx, Austin, Sara Hickman, Sara.com, Sara.net
01/31/2013, TX, Austin, Raina Rose, RainaRose.com, CactusCafe.edu
01/31/2013, TX, Austin, Carrie Elkin, carrieelkin.com, WyldWood.com
This is what is printed to my screen.
Quote:
01/04/2013, Tx, Austin, Lake Street Dive, LSD.com, WhipInn.net 01/31/2013, TX, Austinstring not found01/16/2013, Tx, Huntsville, Becca Loebe, RebeccaLoebe.com, ArHouseConcerts.com 01/31/2013, TX, Austinstring not found12/29/2012, Tx, Hem[steqe, Kendra Smith, KS.com, KS.net 01/31/2013, TX, Austinstring not found02/08/2013, Al, Atlanta, Becca Loebe, RebeccaLoebe.com, EddoesAtoc/cp, 01/31/2013, TX, Austinstring not found01/31/2013, Tx, Austin, Sara Hickman, Sara.com, Sara.net 01/31/2013, TX, Austinstring not found01/31/2013, TX, Austin, Raina Rose, RainaRose.com, CactusCafe.edu 01/31/2013, TX, Austinstring not found01/31/2013, TX, Austin, Carrie Elkin, carrieelkin.com, WyldWood.com 01/31/2013, TX, Austinstring not found01/31/2013, TX, Austinstring not found
 
Old 12-26-2012, 05:21 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Code:
$searchPattern = "01/31/2013, TX, Austin";
Will your search always be the first three comma-delimited data items, as shown in your example?
Or, do you want to search on any number of data items, in any order?

Daniel B. Martin
 
Old 12-26-2012, 07:32 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Dafydd View Post
This is the code adapted for an example of 'strpos' found here: http://www.ehow.com/how_8481508_stri...#ixzz2GBM2EfuW
Well, ehow is confirmed as a horrible site to get information from. First off, obviously you'll want to add newlines to the output, because it's completely unreadable. Now, let's look at the official PHP manual page for strpos(), scroll down to the big red Warning box:
Quote:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
If you follow the link to Booleans section you can see that 0 gets converted into FALSE, so if you find match at the beginning of the string, $locationOfString will == FALSE (but not === FALSE).


Okay, so that should tell how to use strpos() correctly, but if you look at what it does: "Find the ... first occurrence of a substring ..." you might realize that it's doing more than you want. Specifically, you only want to match the beginning of the line, not search the entire string. For this task you should use strncmp().
 
Old 12-26-2012, 07:35 PM   #8
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by danielbmartin View Post
Code:
$searchPattern = "01/31/2013, TX, Austin";
Will your search always be the first three comma-delimited data items, as shown in your example?
Yes.
Quote:
Or, do you want to search on any number of data items, in any order?
Daniel B. Martin
No.
 
Old 12-26-2012, 09:12 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Could you use something as simple as this?

InFile ...
Code:
01/04/2013, Tx, Austin, Lake Street Dive, LSD.com, WhipInn.net
01/16/2013, Tx, Huntsville, Becca Loebe, RebeccaLoebe.com, ArHouseConcerts.com
12/29/2012, Tx, Hem[steqe, Kendra Smith, KS.com, KS.net
02/08/2013, Al, Atlanta, Becca Loebe, RebeccaLoebe.com, EddoesAtoc/cp,
01/31/2013, Tx, Austin, Sara Hickman, Sara.com, Sara.net
01/31/2013, TX, Austin, Raina Rose, RainaRose.com, CactusCafe.edu
01/31/2013, TX, Austin, Carrie Elkin, carrieelkin.com, WyldWood.com
Code ...
Code:
SP="01/31/2013, TX, Austin"  # SP = Search Pattern
grep ^"$SP" $InFile
Result ...
Code:
01/31/2013, TX, Austin, Raina Rose, RainaRose.com, CactusCafe.edu
01/31/2013, TX, Austin, Carrie Elkin, carrieelkin.com, WyldWood.com
Daniel B. Martin
 
Old 12-26-2012, 10:59 PM   #10
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
Grep would be excellent.

I want to do this on my website. Have clients log in and enter the date state and city they are visiting and get back a list of musicians that are playing on the date they will be there.

If 'grep' is a core unit in Linux, how do I call grep from inside a html/php program on my host server that will read the data file?
 
Old 12-27-2012, 07:14 AM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Dafydd View Post
Grep would be excellent.

I want to do this on my website. Have clients log in and enter the date state and city they are visiting and get back a list of musicians that are playing on the date they will be there.

If 'grep' is a core unit in Linux, how do I call grep from inside a html/php program on my host server that will read the data file?
Oh wow, I am way over my depth here. I suggested grep and gave an example but I know almost zero about html or php or servers. Let's hope one of the LQ Gurus steps forward to address those topics.

Daniel B. Martin
 
Old 12-27-2012, 05:50 PM   #12
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Just for fun I implemented the search as a standalone program. You might like to play with it and adapt it to the GUI in your web site.

Input file ...
Code:
01/04/2013, TX, Austin, Lake Street Dive, LSD.com, WhipInn.net
01/16/2013, TX, Huntsville, Becca Loebe, RebeccaLoebe.com, ArHouseConcerts.com
12/29/2012, TX, Hempstead, Kendra Smith, KS.com, KS.net
02/08/2013, GA, Atlanta, Becca Loebe, RebeccaLoebe.com, EddoesAtoc/cp,
01/31/2013, TX, Austin, Sara Hickman, Sara.com, Sara.net
01/31/2013, TX, Austin, Raina Rose, RainaRose.com, CactusCafe.edu
01/31/2013, TX, Austin, Carrie Elkin, carrieelkin.com, WyldWood.com
The program, in its entirety ...
Code:
#!/bin/bash
#  Daniel B. Martin   Dec12
#
#  To execute this program, launch a terminal session and enter:
#  bash /home/daniel/Desktop/LQfiles/dbm568.bin
#
#  This program inspired by
#  http://www.linuxquestions.org/questions/programming-9/
#    what-kind-of-search-routine-do-i-need-4175442825/
 
Path='/home/daniel/Desktop/LQfiles/dbm568'
InFile=$Path'inp.txt'
  CDLs=$Path'cdls.txt'

# Construct a list of concert dates/locations.
# Format of date field is mm/dd/yyyy
cut -d\, -f1-3 $InFile         \
|sort -n -k1.7 -k1.1 -k1.4 -u  \
|nl -s" "                      \
> $CDLs                          

# Solicit user input: select date/location.
echo; echo "Concert dates and locations:"; cat $CDLs
echo
echo "Please enter the NUMBER which identifies the date/location of interest."
read CDLnum

# Establish the Search Pattern.
SP=$(sed $CDLnum'q;d' $CDLs  \
|sed 's/^[ \t]*//'           \
|cut -d" " -f2-) 

# Check the calendar for matches on the Search Pattern.
echo; echo "Concert dates matching" $SP
grep -i ^"$SP" $InFile  # Check the calendar for matches
echo

exit
I didn't bother with niceties such as range-checking the user's numerical input. No need for that if your GUI invites the user to make a selection from a pull-down menu.

Daniel B. Martin
 
1 members found this post helpful.
Old 12-29-2012, 05:08 PM   #13
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
This code will select the desired date row and present it to the screen.

Code:
<?php
	$file = fopen("WhereAmIPlaying", "r") or exit("Unable to open file!");
	$searchPattern = '01/31/2013, TX, Austin';

	while(!feof($file))
	  {
	 	 $stringToSearch = fgets($file);
//				printf('%s', $stringToSearch); // test for data
//				printf('%s', $searchPattern);  // test for data
    		$locationOfString = stripos($stringToSearch , $searchPattern);

			if($locationOfString !== false) 
			{ 
				printf('%s', $stringToSearch);
				echo "<br>";  
			}
	 }
	fclose($file);
?>
You can view it in action here.
Marking this thread solved.
 
  


Reply



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
Routine maintenance launch6221 Linux - Newbie 20 12-04-2009 06:03 PM
Just a routine problem. wpbest Linux - Newbie 2 06-08-2008 09:21 PM
Interrupt routine... Elric of Grans Programming 2 07-05-2005 07:11 PM
About startup routine eshwar_ind Programming 18 05-23-2004 02:01 AM
kind of a programming quesion...kind of not tho jhorvath Programming 2 06-30-2003 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 PM.

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
Open Source Consulting | Domain Registration