Solved

grep in shell

Posted on 1998-10-29
10
570 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
  • 5
  • 2
  • 2
  • +1
10 Comments
 

Author Comment

by:khoty
Comment Utility
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
Comment Utility
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
Comment Utility
awk '/^Name/{n=$0}/DOB{d=$0}/Address/{a=$0}/Grose/{print n; print d; print a; exit}' your_file
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 400
0
 

Author Comment

by:khoty
Comment Utility
Adjusted points to 800
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 800 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now