Matching a string (zip code) from a list in a separate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching a string (zip code) from a list in a separate file
# 1  
Old 12-10-2009
Matching a string (zip code) from a list in a separate file

I have a list of postal addresses and I need to pull the records that match a list of zip codes in a separate file. The postal addresses are fixed width. The zip code is located in character position 149-157.

Something better than:
Code:
cat postalfile.txt | grep -f zipcodes.txt

would be great.

Code:
$ cat postalfile.txt |head -3
MS         JOAMARA        ARTE              44 SECTOR LA PLAYITA                            ADJUNTAS     PR005412312
MS         JULIA          ALIFEA            44 CALLE JOHN F KENNEDY                         ADJUNTAS     PR006012
MS         MARCENE        SERVANA           44 CALLE SAN JOAQUIN                            ADJUNTAS     PR006012137

Thanks
# 2  
Old 12-10-2009
what exactly are you looking for? to know which ones items match, or which ones don't?
# 3  
Old 12-10-2009
Quote:
Originally Posted by sitney
Something better than:
Code:
cat postalfile.txt | grep -f zipcodes.txt

would be great.
Code:
grep -f zipcodes.txt postalfile.txt

Smilie

The codes on position 149-157 are missing. Perhaps you could post a sample zipcodes.txt as well?

S.
# 4  
Old 12-10-2009
Quote:
Originally Posted by sitney
Something better than:
Code:
cat postalfile.txt | grep -f zipcodes.txt

would be great.
Why better than that :
Code:
grep -f zipcodes.txt postalfile.txt

# 5  
Old 12-11-2009
I want something that looks specifically at the zip code position chars 149-157 because there will be addresses with 5 digit numbers in them that will match against a general grep statement. As in the following examples:

Code:
MS         MARLENE       SERBRANO           14044 W 174TH ST          APT 7B                        BRONX        NY10453
MS         YANIRA        GAPONTE            34126 CRESCENT DR                                       CHICOPEE     MA01013
MS         BETZAIDA      JACOBSEN           41102 HAMILTON AVE         APT 3                         LYNN         MA01902

# 6  
Old 12-11-2009
I think than that the best is to check line by line and grep the extracted zip code in the other file like
Code:
while read LINE
do
    ZIP=${LINE:148:5} # first pos. has index 0
    if grep -f $ZIP zipcodes.txt
    then
        ....
    fi
done < postalfile.txt

# 7  
Old 12-11-2009
Try this, but looks like your zip number char position in the file pasted does not seem to be correct:

If my understanding is correct, your zip file contains only zip codes and dat_file conatins address with zip code at char position149-157.

Code:
awk 'NF==1 { ++A[$0] } NF>1 {s=substr($0,149,8)} A[s]'  zip_file dat_file


Last edited by dennis.jacob; 12-11-2009 at 03:20 AM.. Reason: Explained my understanding of the problem..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

2. UNIX for Advanced & Expert Users

Wanted to replace string in an .xlsx file in multiple ZIP Files

Hi , I am having a ZIP file containing an .xlsx file . Now i wanted to replace "GJ" to blank in the .xlsx file . I tried using the below code but not working , Please guide : #!/bin/bash log="/home/srikant/scripts/replacescriptFHO.log" date > $log echo "" >> $log echo initiating for FHO... (1 Reply)
Discussion started by: vipinmaster
1 Replies

3. Shell Programming and Scripting

Greping frequency for list of phrases is a separate file

Dear All I have a big set of data which I would like to summerize to have a better sense of it the problem is like this ... I have more than 200 files each related to a person which includes different sentences with specific elements (files in a directory) e.g. mark (one file in the directory)... (9 Replies)
Discussion started by: A-V
9 Replies

4. Shell Programming and Scripting

Compare two string in two separate file and delete some line of file

Hi all i want to write program with shell script that able compare two file content and if one of lines of file have # at the first of string or nothing find same string in one of two file . remove the line in second file that have not the string in first file. for example: file... (2 Replies)
Discussion started by: saleh67
2 Replies

5. Shell Programming and Scripting

Matching string from input to string of file

Hi, i want to know how to compare string of file with input string im trying following code: file_no=`paste -s -d "||||\n" a.txt | cut -c 1` #it will return collection number from file echo "enter number" read " curr_no" if ; then echo " current number already present" fi ... (4 Replies)
Discussion started by: a_smith
4 Replies

6. UNIX for Dummies Questions & Answers

search for a string in zip file

Hi, I know gzcat helps grep for a string inside a file that has been zipped using gzip command. However, how can the same be achieved for files zipped using zip command and any other compression commands you may know off. Thanks, Mohtashim (5 Replies)
Discussion started by: mohtashims
5 Replies

7. Shell Programming and Scripting

Specific file list to be zip

Hi, I need a specific list of files to be zip automatically. based on the criteria Criteria: 1. It should not be the current file and not less than 10 files e.g in a folder contails 100 files jan 50 -> contains ->45 zip files e.g. XXX.gz 5 normal log files e.g XXX.log ... (11 Replies)
Discussion started by: jagkoth
11 Replies

8. Shell Programming and Scripting

Renaming files by matching info from a separate file

Hi All, I could use a bit of help with this as I'm at a loss. I have a number of files all named accordingly: I have a separate text file that is as follows: What I want to do is end up with: I don't know how to do this, though I'm certain it can be done, and would rather learn how... (14 Replies)
Discussion started by: Demosthenes
14 Replies

9. UNIX for Dummies Questions & Answers

tar, zip multiple separate directories and move the results to another volume

TIA, I'm using FreeBSD 6 I have a series of Directories (A,B,C,...Z). Each directory has files and other directories within it. I want to compress the contents of each top directory into a single file so that I get an archive of each directory (for example, A.gzip) AND and want to move... (5 Replies)
Discussion started by: jccbin
5 Replies

10. UNIX for Dummies Questions & Answers

unzip .zip file and list the files included in the .zip archive

Hello, I am trying to return the name of the resulting file from a .zip archive file using unix unzip command. unzip c07212007.cef7081.zip Archive: c07212007.cef7081.zip SecureZIP for z/OS by PKWARE inflating: CEP/CEM7080/PPVBILL/PASS/G0063V00 I used the following command to unzip in... (5 Replies)
Discussion started by: oracledev
5 Replies
Login or Register to Ask a Question