Computing.Net > Forums > Unix > Help: how do i search a file?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help: how do i search a file?

Reply to Message Icon

Name: jasonjason
Date: February 28, 2003 at 17:52:49 Pacific
OS: Linux, unix
CPU/Ram: n/a
Comment:

hi,

i want to find matched cases in my phonelist.txt file, and then save the results in an array so i can process them.

for example, the phonelist.txt file consists of different fields separated by semicolons as:

jon;555-5555;jon@hotmail.com
kathy;444-4444;kathy@hotmail.com
mark;333-3333;mark@hotmail.com
jon;111-1111;jon@hotmail.com

how do i conduct a search by name, by phone number, or by email address and then store the matched cases in an array?

if i search for jon, then i should get two results, and i want to process these two results.

thanks,

jason



Sponsored Link
Ads by Google

Response Number 1
Name: jasonjason
Date: February 28, 2003 at 17:56:40 Pacific
Reply:

i need to clarify something:

after the search, i want to save the entire record (that contains name, phone #, email address) of each result. for example, if i search for jon, then i should get two records of jon. then i can process each record accordingly.

thanks,

jason


0

Response Number 2
Name: Don Arnett
Date: February 28, 2003 at 18:57:51 Pacific
Reply:

You don't specify a language preference, so I'm going to pick the easiest for the task described. But depending on exactly what you want to do, there may be better answers.

grep is a shell script command for search files for specified text.

> grep jon phonelist.txt
jon;555-5555;jon@hotmail.com
jon;111-1111;jon@hotmail.com


The command above searches for the string 'jon' in the file 'phonelist.txt'. Any lines containing 'jon', anywere in the line, are output.

That's a pretty simple answer, but it answers the question that you asked.


0

Response Number 3
Name: deeps
Date: March 3, 2003 at 06:07:31 Pacific
Reply:

Hi Jason,
I suggest you use Awk. It's the best, when the no of records involved are more. Grep has a limitation as it does scan the whole file once for each string you search.

Awk is powerful in a way that it works line by line.

So, in you case if you had to use awk it would be like this:

awk -F";" '$1=="The name you want"{print $0}' phonelist.txt

Thereafter you could put conditions like AND , OR along with conditional statments of $1 above and get the required result.

For storing in an array it's as simple as you do in C language.

So go ahead anf start scripting :)

~deeps


0

Response Number 4
Name: jasonjason
Date: March 4, 2003 at 14:49:25 Pacific
Reply:

thanks to deeps and don,

i'd appreciate your help!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


store a line from file in... multiple IP



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help: how do i search a file?

How do I format a DOS Partition Under SC www.computing.net/answers/unix/how-do-i-format-a-dos-partition-under-sc/1938.html

How do I get a OCE9400 Plotter Scaner to www.computing.net/answers/unix/how-do-i-get-a-oce9400-plotter-scaner-to/1990.html

How do i recover a deleted file in unix? www.computing.net/answers/unix/how-do-i-recover-a-deleted-file-in-unix/2325.html