Solved

grep in shell

Posted on 1998-10-29
10
578 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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 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.

786 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