Solved

grep in shell

Posted on 1998-10-29
10
579 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 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