awk and grep to search a data file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk and grep to search a data file
# 1  
Old 01-18-2010
awk and grep to search a data file

Hi everyone,

I cannot figure out how I can do a search in a file that has Names, Surnames, Addresses and telephone number of a number of people.
Here is an example of the data file
Code:
Daisy:Hunter:490 London Road:07313196347
Richard:Murphy:983 Main Road:07002625997
Isobel:Magnusson:133 London Road:01036429709
Jim:Yates:421 Dickenson Road:07622293238
Jenny:Yates:467 Victoria Place:01278874246
Daisy:Edwards:66 Daisy Avenue:01852487687
Ruth:Taylor:592 Dickenson Road:01869919954
Jenny:Yates:941 Wilmslow Road:01681041319
Adam:Smith:946 Main Road:07914868675
Richard:Richards:88 Birch Grove:01264290192
Jenny:Taylor:217 Victoria Place:07337349573
Jim:Jones:337 Wilmslow Road:07404751140
Fred:Jones:935 Dickenson Road:07435197378
Ruth:Murphy:336 Summer Place:07015113018

The data file is called addresses,
I want using awk programming system and grep to do the following two things.
1. Put all the lines that have Daisy as the name of the person in a file called daisy_name.txt
2. Put all the lines that have Daisy as the name of the street (address) in a file called daisy_address.txt

I cannot use a simple
Code:
grep -i "daisy" < addresses > daisy_name.txt

because some lines have Daisy as the name of the person and some other lines as the name of the address street. Smilie

Please Help me if you can.

Thanks guys Smilie

---------- Post updated at 09:09 AM ---------- Previous update was at 08:26 AM ----------

Hi guys,

I have just found that this can be done without grep!!
So here is my solution

Code:
awk '$1 ~ "Daisy"' addresses > daisy_name.txt
awk '$2 ~ "Daisy"' addresses > daisy_address.txt

SmilieSmilieSmilieSmilie
# 2  
Old 01-18-2010
You can do both in a single command and you need to specify field delimiter as colon.

Code:
awk -F":" '$1 ~ "Daisy" { print > "daisy_name.txt" } $2 ~ "Daisy" { print > "daisy_address.txt" } ' addresses

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

4. Shell Programming and Scripting

awk or grep to search one column and output the other

Hello, it would be great if someone can help me with the following: I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched? cat fileA... (7 Replies)
Discussion started by: Manyaka
7 Replies

5. UNIX for Dummies Questions & Answers

One liner pattern search with awk/sed/grep

I have an array containing bunch of characters. I have to check this array for specific character and if "Not Found than" use a goto statement to go to USAGE set options = (A B C D E F) @ i = 0 while ($i <= ${#options}) if ($options != "F" || $options != "D") then goto USAGE endif @... (1 Reply)
Discussion started by: dixits
1 Replies

6. Shell Programming and Scripting

Router ping log extract data from it Awk/Sed/grep

Hi, I am new to this world.. Using expect i loging to router and checking ping response to my links. I need to genarate report using this output and that report contains only three file link name, packet loss, latency. my output of script is like below: -bash-3.00$ monmw/mwbkp... (2 Replies)
Discussion started by: jkmistry
2 Replies

7. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

8. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

9. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

10. Shell Programming and Scripting

Big data file - sed/grep/awk?

Morning guys. Another day another question. :rolleyes: I am knocking up a script to pull some data from a file. The problem is the file is very big (up to 1 gig in size), so this solution: for results in `grep "^\ ... works, but takes ages (we're talking minutes) to run. The data is held... (8 Replies)
Discussion started by: dlam
8 Replies
Login or Register to Ask a Question