julius cesar
julius cesar

Reputation:

Search then Extract

I have a text file with multiple records. I want to search a name and date, for example if I typed JULIUS CESAR as name then the whole data about JULIUS will be extracted. What if I want only to extract information?

Record number: 1
Date: 08-Oct-08
Time: 23:45:01
Name: JULIUS CESAR
Address: BAGUIO CITY, Philippines
Information:
I lived in Peza Loakan, Bagiou City
A Computer Engineering student
An OJT at TIPI.
23 years old.

Record number: 2
Date: 09-Oct-08
Time: 23:45:01
Name: JOHN Castro
Address: BAGUIO CITY, Philippines
Information:
I lived in Peza Loakan, Bagiou City
A Electronics Comm. Engineering Student at SLU.
An OJT at TIPI.
My Hobby is Programming.


Record number: 3
Date: 08-Oct-08
Time: 23:45:01
Name: CESAR JOSE
Address: BAGUIO CITY, Philippines
Information:
Hi,,
I lived Manila City
A Computer Engineering student
Working at TIPI.

Upvotes: 0

Views: 170

Answers (2)

Robert Elwell
Robert Elwell

Reputation: 6668

In PHP, you can run a SQL select statement like:

"SELECT * WHERE name LIKE 'JULIUS%';"

There are native aspects of PHP where you can get all of your results in an associative array. I'm pretty sure it's ordered by row order. Then you can just do something like this:

echo implode(" ", $whole_row);

Hope this is what you're looking for!

Upvotes: -1

user19302
user19302

Reputation:

If it is one line per entry, you could use a regular expression such as:

$name = "JULIUS CESAR";

Then use:

/$name/i 

to test if each line is about "JULIUS CESAR." Then you simply have to use the following regex to extract the information (once you find the line):

/Record number: (\d+) Date: (\d+)-(\w+)-(\d+) Time: (\d+):(\d+):(\d+) Name: $name Address: ([\w\s]+), ([\w\s]+?) Information: (.+?)$/i

$1 = record number

$2-$4 = date

$5-$7 = time

$6 = address

$7 = comments

I would write a code example, but my perl is rusty. I hope this helps :)

Upvotes: 2

Related Questions