?
Solved

grep in shell

Posted on 1998-10-29
10
Medium Priority
?
583 Views
Last Modified: 2011-09-20
I have a file in the following format:

Name
DOB
Address

sometimes the DOB is missing so I may only have

Name
Address

eg .
Name    Terry Likos
DOB     7/2/1971
Address 12 Grose street etc
Name   John Doe
Address 134 Grose street etc

so if I want to do a grep for 'Grose' then I want to get the
name and DOB(if there is any) and that person's address.
Is there a way to do this in a shell script? (my name and address file is almost 12MB so I want to do it fast)
0
Comment
Question by:khoty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:khoty
ID: 2007639
There is one more little thing...
if I type 'Terry' and 'John' then I want to see if they live in the same street, as in the above example 'Grose street'.
So basically we want find all those people that live in a particular street either searching by street name , or searching by names. However the format of the result(s) returned will be the same ie.name and DOB(if there is any) and that person's address.
0
 
LVL 5

Expert Comment

by:bchew
ID: 2007640
Not with grep!  Maybe with awk.  Take a look at "man awk" to see if it will do what you want.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2007641
awk '/^Name/{n=$0}/DOB{d=$0}/Address/{a=$0}/Grose/{print n; print d; print a; exit}' your_file
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 2007642
I don't think that awk script is quite right.
Consider the a file like:

Name    Terry Likos
DOB     7/2/1971
Address 12 Rose street etc
Name   John Doe
Address 134 Grose street etc

Also, consider what would happen if you searched for /John/
0
 

Author Comment

by:khoty
ID: 2007643
awk '/^Name/{n=$0}/DOB{d=$0}/Address/{a=$0}/Grose/{print n; print d; print a; exit}' temp
awk: syntax error near line 1
awk: bailing out near line 1

There is ALSO one more little thing...
if I type 'Terry' and 'John' then I want to see if they live in the same street, as in the above example 'Grose street'.
So basically we want find all those people that live in a particular street either searching by street name , or searching by names. However the format of the result(s) returned will be the same ie.name and DOB(if there is any) and that person's address.

0
 
LVL 84

Expert Comment

by:ozo
ID: 2007644
I think ahoffmann meant to type
awk '/^Name/{n=$0}/DOB/{d=$0}/Address/{a=$0}/Grose/{print n; print d; print a; exit}'
Although that still has the minor problem mentioned earlier.


awk 'BEGIN{ORS=";"}/^Name/{printf "\n"}{print}' your_file | egrep 'Terry|John' | tr ';' '\012'
0
 

Author Comment

by:khoty
ID: 2007645
how can I optimise awk 'BEGIN{ORS=";"}/^Name/{printf "\n"}{print}' your_file | egrep 'Terry|John' | tr ';' '\012'
this one is good...but runs very slow when I test it with a 12MB data file.
0
 

Author Comment

by:khoty
ID: 2007646
Adjusted points to 400
0
 

Author Comment

by:khoty
ID: 2007647
Adjusted points to 800
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1600 total points
ID: 2007648
ozo, you're right: i missed the final / for DOB. Sorry for that.

To get all occurances of "Grose" just use my awk without the final exit command.

> awk 'BEGIN{ORS=";"}/^Name/{printf "\n"}{print}' your_file | egrep 'Terry|John' | tr ';' '\012'

Hmm, this did not what you want as described in your initial question.
It's slow because of ORS, which makes your output single-lined piped to egrep which starts its work when recieving the first \n
You may improve it as follows:

  awk '/^Name.*Terry/{print}/^Name.*John/{print}' your_file
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question