Searching data files for another file of values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching data files for another file of values
# 1  
Old 12-15-2009
Searching data files for another file of values

I've used awk for some simple scripting, but having trouble figuring out how to search a couple of data files that have Name/Address/Zip Codes from another file that has list of only Zip Codes, and write out the lines that matched.

Zip code field in the data file is 27

I was thinking something like this:
Code:
for datafil in 'ls filelocation'

do
    #set zip code variable
    zipcode='cat zipcodes.txt'
    
    #search each file and write the matches
    cat $datafil | awk 'BEGIN {FS="\t";OFS="\t";}{if ($27==$zipcode) print $0;} > output.txt
    
done

Am I on the right track with this? Seems like it should be easy, but it's not working

Thanks,
-Michael
# 2  
Old 12-15-2009
hello
why not use grep -f aka fgrep like this
Code:
fgrep pattern_file target_file 
or grep -f pattern_file target_file

Regards.
# 3  
Old 12-15-2009
Hi.

Assuming you want to print all addresses in the addresses file with zip codes matched in the zip codes file, something like:

Code:
awk '
  NR == FNR { ZIP[$1] = 1; next }
  ZIP[$27]
' zips.txt adds.txt

(or grep as gaurav1086 suggests... should work assuming there's not a house numbered like a zip code!)
# 4  
Old 12-15-2009
Thanks for the quick reply. I thought about grep, but because it has to specifically only match on the zip code field, grep would match on that string through the whole record, so it wouldn't quite work.

---------- Post updated at 03:54 PM ---------- Previous update was at 03:49 PM ----------

Thanks for the reply - how could I change this to incorporate multiple data files that I'm searching against? Other than running it multiple times Smilie

Quote:
Originally Posted by scottn
Hi.

Assuming you want to print all addresses in the addresses file with zip codes matched in the zip codes file, something like:

Code:
awk '
  NR == FNR { ZIP[$1] = 1; next }
  ZIP[$27]
' zips.txt adds.txt

(or grep as gaurav1086 suggests... should work assuming there's not a house numbered like a zip code!)
# 5  
Old 12-15-2009
Assuming that a zip-code file has only one column:

Code:
awk '
  NF == 1 { ZIP[$1] = 1; next }
  ZIP[$27]
' zips*.txt adds*.txt address1.txt address2.txt ......

If you have too many, use cat filenames | awk ... instead.
# 6  
Old 12-15-2009
Thanks, I just used a directory/*.txt for the data files, and that worked well.

I appreciate the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for values in a file

Hi guys. I'm trying to do a search on the fruit & brand inside Fruit.txt, and printing the result out in the following format: , , $, I am able to do this via the following code: awk -F: -vOFS=", " -vt="$Fruit:$Brand" '$0~t{$3="$"$3;print}' Fruit.txt However, I want to be able to... (5 Replies)
Discussion started by: todaealas
5 Replies

2. UNIX for Dummies Questions & Answers

Need help searching for values in file then adding to line

Hello! I'm currently trying to organize data for some bio research, but I'm not sure how to compare a value to values in a file. So what I have are 2 arrays, one array contains NM numbers and can be referenced as NM. The other array has symbols, SYM. I have a file for which it contains an NM... (1 Reply)
Discussion started by: ShiGua
1 Replies

3. Shell Programming and Scripting

Fetch the different data by searching with a same variable from a file in AIX server

Hi, I am trying to fetch the different values in an xml file by searching with the same variable in AIX Server. <name>SharedResources/Shared/JNDI/Username</name> <value>admin</value> <name>SharedResources/Shared/JNDI/Username</name> ... (1 Reply)
Discussion started by: tejastrikez
1 Replies

4. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

5. Shell Programming and Scripting

Divide data with specific column values into separate files

hello! i need a little help from you :) ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example. ... (3 Replies)
Discussion started by: chanclitas
3 Replies

6. Shell Programming and Scripting

data searching and pasting with in the file

Hi All, I have .csv file in which I am trying to manipulate a column date, I started with awk but i am not sure how to do the below logic in . the file has a 23 columns and in the first row if the value is Trend and in the second column the value is Analysis then the program has to... (3 Replies)
Discussion started by: shruthidwh
3 Replies

7. Programming

searching files for hex or oct values

I have a set of files without extensions. How can I programatically tell if a file is in gzip format? The gzip file format spec RFC 1952 GZIP File Format Specification version 4.3 states that gzip files have certain hex/oct values at the beginning of the file. 1st byte = 0x1f in hex,... (2 Replies)
Discussion started by: daflore
2 Replies

8. Shell Programming and Scripting

Searching a particular string with spaces in a data file

Hi, I'm new to shell scripting and require your help in achieving the requirement. I have a data file which stores organization name as one of the column data in a csv data file. Organization name stored in data file is: Canadian OU CAD Sample Data file: 1,5,4,5,, ... (9 Replies)
Discussion started by: andy4013
9 Replies

9. Shell Programming and Scripting

Comparing data in file with values in table

Hi, I want to calculate the number of pipe delimiters in a file for all lines seperately. For eg:i have a file Project.txt Mohit|chawla|123|678 File1|File2|345|767|678 And my file contains many lines like this it shd give me the output as 4 5 or give me the output for all the... (0 Replies)
Discussion started by: Mohit623
0 Replies

10. UNIX for Dummies Questions & Answers

Searching list of entries in file for actual files in dir

Hi all, I have a file entries.txt that contains a number of entries all on seperate lines e.g. test1 test2 test3 test4 Then in a directory called /TestFiles I have a number of files that could contain the above text in the file name e.g. qwertytest1.csv qwertytest2.csv... (2 Replies)
Discussion started by: not4google
2 Replies
Login or Register to Ask a Question