Replacing data in one file with data in another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing data in one file with data in another
# 1  
Old 05-04-2012
Replacing data in one file with data in another

Hello,

I have 2 files delimited by "|".

File1:

Code:
1|New York
12| Buffalo
599| Syracuse


File2:

Code:
56 Kennedy |1
9 Burridge Pl|15
98 BELL ROCK |599


My goal: Is to replace the numerical numbers in "File 2" (second field, not street address) with the corresponding city names from "File 1" where the second field in file 2 matches the first field in file 1. Many in the run of the larger list will not match and should just be ignored. In other words for the 3 records in each file listed above only 2 records should display and should ultimately > forwarded to a new file that looks like:

Code:
56 Kennedy | New York
98 BELL ROCK | Syracuse

Delimiters should remain. I really need to do this with text utilities before data gets imported. Most likely I may need to add a 3rd and 4th field as a delimiter to file 1 for "state" and "zip code". Just wanted to mention that. I assume I can just do that after I get the finished file by doing something like:

Code:
awk -F\| '{print $1,$2"||"}' newfile

I have awk, sed, cut etc. to get things this far and in numerical incrementing fields. I am just having a tough time sealing the deal. Any help appreciated. It's been a while for me battling it out with the old text utils! I realize this is easier with something like mysql or VB but I need to take care of it before importing to anything else.


Thanks in advance for any replies!


Art

Last edited by Scrutinizer; 05-05-2012 at 06:27 AM.. Reason: code tags
# 2  
Old 05-05-2012
Actually, pretty straight forward in awk:

Code:
awk -F \| '
    NR == FNR { city[$1] = $2; next; }
    $2 in city { printf( "%s|%s\n", $1, city[$2] ); }
' file1 file2



It can easily be expanded to include state and zip, or just add them as space separated tokens after the city:

Code:
1|New York, New York 10092

# 3  
Old 05-05-2012
Awesome!

Thank you so much. I have not programmed in quite a while and I was really spinning my wheels on this one. I was never as proficient with awk as I should be. I really need to practice that as it is very powerful and much of what I do for projects always seems to involve more advanced awk commands at one point or another. I had one other issue but will probably start a new thread so keep this one solved, short and to the point.

Thanks again! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. 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

3. Shell Programming and Scripting

Generate tabular data based on a column value from an existing data file

Hi, I have a data file with : 01/28/2012,1,1,98995 01/28/2012,1,2,7195 01/29/2012,1,1,98995 01/29/2012,1,2,7195 01/30/2012,1,1,98896 01/30/2012,1,2,7083 01/31/2012,1,1,98896 01/31/2012,1,2,7083 02/01/2012,1,1,98896 02/01/2012,1,2,7083 02/02/2012,1,1,98899 02/02/2012,1,2,7083 I... (1 Reply)
Discussion started by: himanish
1 Replies

4. Shell Programming and Scripting

Replacing Data in a File

Hi All, Have a requirement where i needs to alter the Content of File once it is generated. The File consist of Multiple line ...range from 2000-8000 lines. I need to change the Content of Lines which match this Pattern ... (14 Replies)
Discussion started by: asheshrocky
14 Replies

5. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

6. UNIX for Dummies Questions & Answers

How to get data only inside polygon created by points which is part of whole data from file?

hiii, Help me out..i have a huge set of data stored in a file.This file has has 2 columns which is latitude & longitude of a region. Now i have a program which asks for the number of points & based on this number it asks the user to enter that latitude & longitude values which are in the same... (7 Replies)
Discussion started by: reva
7 Replies

7. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

8. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies

9. Shell Programming and Scripting

Replacing the last data of each line ina file

Hi, I ahve a file where the format is as given below: qqq dfsf gdfjkg gjdosjg jkflsjd 21 fdksdlf fsdnkfl lkdfsgjld 45 laefsdl fsdlfksl fsdklflk gfsdl 56 I need to replace the last number by space or delete the last number in each line. (by using... (6 Replies)
Discussion started by: jisha
6 Replies
Login or Register to Ask a Question