Replace field in one file with whole record data of another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace field in one file with whole record data of another
# 1  
Old 05-05-2012
Replace field in one file with whole record data of another

Hello Group,

I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field 3 gets replaced.

First I need to convert “File 2” to have only the first letter of each word capitalized. I know I can:
Code:
 tr '[:upper:]' '[:lower:]' < file2 > outfile

to get everything to lower case but I am lost after that to convert back just the first alphabetic character in each word, in each field of “File 2” to capitals.

Here is the sample data I need to convert below:

File1:
Code:
 10 MISC 400000|03/01/2012|Syracuse|53-55 Main St. Ave|Lisa Thomas|Tom Alexander
 10 MISC 400012|03/01/2012|Buffalo|327 Main Street|Robin Lake|Anita Parker
 10 MISC 460316|03/01/2012|New York|82-84 Broadway, Unit 84|John Smith|Joe Jones

File2:
Code:
 NEW YORK|NY|01234|COUNTY1|978|42.562912|-71.568824
 SYRACUSE|NY|05678|COUNTY2|781/617|42.298800|-71.260100
 BUFFALO|NY|09112|COUNTY3|978|42.600122|-72.086316


So with that said the result of conversion of “File 2” + replacing field 3 in “File” should ultimately look like the below (Newfile).


Newfile:
Code:
 10 MISC 400000|03/01/2012|Syracuse|Ny|05678|County2|781/617|42.298800|-71.260100|53-55 Main St. Ave|Lisa Thomas|Tom Alexander
 10 MISC 400012|03/01/2012|Buffalo|Ny|09112|County3|978|42.600122|-72.086316|327 Main Street|Robin Lake|Anita Parker
 10 MISC 460316|03/01/2012|New York|Ny|01234|County1|978|42.562912|-71.568824|82-84 Broadway, Unit 84|John Smith|Joe Jones


I know most likely this is another job for “awk” but I am way out of practice.


Thanks in advance for any replies!


Art

Last edited by Scrutinizer; 05-06-2012 at 02:24 AM.. Reason: code tags
# 2  
Old 05-06-2012
It think perl or GNU sed are more suited for uppercase / lowercase conversion. Give this try:
Code:
perl -pe 's/\b(.)(.*?)\b/$1\L$2/g' file2 |
awk 'NR==FNR{A[$1]=$0; next}$3 in A {$3=A[$3]}1' FS=\| OFS=\| - file1

This is assuming that the leading space in the files is not there in reality. This can be done in regular awk, but that leads to elaborate code. This can also be combined in one Perl, but I think this might do...

Last edited by Scrutinizer; 05-06-2012 at 03:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace value in each field until a certain character in each record?

Each record coming with column names. I have to replace them in each record as shown below TIME=20181219110000261|CHAN=FMBKHJBAAAADPCFNAAAAAABA|EVNT=SWIclst|VALU=Session FMBKHJBAAAADPCFNAAAAAABA started|SRC=NSS|UCPU=0|SCPU=0 Output should look like: ... (9 Replies)
Discussion started by: sudhakar1987
9 Replies

2. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

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

6. Shell Programming and Scripting

Replace a data in a field if that does not contain a particular pattern

Hi, I have a date/time field in my file. I have to search in all the records and append a timestamp to it, if the timestamp is missing in that field. Is there a possible awk solution for this? Field date format File1 ==== 1|vamu|payer|2007-12-02 02:01:30|bcbs|... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

File Processing - How to Replace particular record

I have a file with data as 1 L 1 Used 1 1 1 1 L ... (6 Replies)
Discussion started by: karumudi7
6 Replies

8. Shell Programming and Scripting

Replace comma by space for specified field in record

Hi, i want to replace comma by space for specified field in record, i mean i want to replace the commas in the 4th field by space. and rest all is same throught the record. the record is 16458,99,001,"RIMOUSKI, QC",418,"N",7,EST,EDT,902 16458,99,002,"CHANDLER,... (5 Replies)
Discussion started by: raghavendra.cse
5 Replies

9. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

10. Shell Programming and Scripting

how to replace field for each record

Hello, I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record. For example: <XMLFORMAT> <customernumberR11>9</customernumberR11> ... (12 Replies)
Discussion started by: happyv
12 Replies
Login or Register to Ask a Question