Replacing first field of file2 with the second filed of file1 for matching cases


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing first field of file2 with the second filed of file1 for matching cases
# 1  
Old 11-06-2014
Replacing first field of file2 with the second filed of file1 for matching cases

Dear All,

Need your help..Smilie

I am not regular on shell scripts..Smilie

I have 2 files..


Content of file1

Code:
cellRef 4};"4038_2_MTNL_KALAMBOLI"
 cellRef 1020};"4112_3_RAINBOW_BLDG"
 cellRef 134};"4049_2_TATA_HOSPITAL"
 cellRef 1003};"4242_3_HITESH_CONSTRUCTION"
 cellRef 744};"5035_2_SHAHI_NOORANI_MASJID"
 cellRef 622};"4614_3_SHYAM_NIVAS_DCS"
 cellRef 1080};"4303_1_LODHA_HEAVEN"
 cellRef 394};"4502_1_NAVKANTLAL"
 cellRef 1884};"4323_2_COMMERCE_CENTER"
 cellRef 888};"4260_1_BOLENATH_DAIRY"
 cellRef 945};"4240_1_SHARIFA_MANZIL"
 cellRef 922};"4231_2_NASHEMAN"
 cellRef 812};"4235_2_SAIDAVILA"
 cellRef 1068};"4613_1_BALRAM_SADAN"
 cellRef 75};"4050_1_MTNL_TALOJE"
 cellRef 250};"4170_3_SANDEEP_APT"
 cellRef 2530};"4015_3_DRONAGIRI"
 cellRef 2355};"4245_3_SAI_PRASAD_KOPRA"
 cellRef 1661};"4210_1_AFZAL_COMPOUND"
 cellRef 765};"4116_3_MARUTI_TOWER"
 cellRef 2415};"4401_2_KALYAN_RAIL_SDCS"
 cellRef 983};"4237_1_AMAL_APARTMENT"
 cellRef 580};"4344_1_CHAMUNDA_DARSHAN"
 cellRef 2437};"4600_1_YOGESHWAR_CHS"
 cellRef 2475};"4136_2_MTNL_APMC"

Content of file2


Code:
cellRef 2530};disabled;main;0;"Trx4";
 cellRef 2530};disabled;main;0;"Trx2";
 cellRef 2355};disabled;main;12;"Trx3";outer
 cellRef 765};disabled;main;11;"Trx2";
 cellRef 580};disabled;secondary;0;"Trx5";inner
 cellRef 580};disabled;secondary;0;"Trx6";inner
 cellRef 2437};disabled;main;-1;"Trx1";
 cellRef 2437};disabled;main;-1;"Trx3";
 cellRef 2437};disabled;main;-1;"Trx2";
 cellRef 2437};disabled;main;-1;"Trx4";
 cellRef 2475};disabled;main;6;"Trx3";outer
 cellRef 2305};disabled;main;6;"Trx2";
 cellRef 2368};disabled;main;0;"Trx4";outer
 cellRef 2368};disabled;secondary;0;"Trx5";inner
 cellRef 2368};disabled;secondary;0;"Trx6";inner
 cellRef 956};disabled;secondary;4;"Trx5";inner
 cellRef 956};disabled;secondary;3;"Trx6";inner
 cellRef 932};disabled;secondary;6;"Trx5";inner
 cellRef 932};disabled;secondary;5;"Trx6";inner
 cellRef 864};disabled;secondary;4;"Trx5";inner

Both the files are semicolon separated..

First fields of both the files are common..

I want a script which will replace first field of file2 with the second field of file1 when first field of both the files matches..
# 2  
Old 11-06-2014
You could try something like:
Code:
awk '
BEGIN {	FS = OFS = ";"
}
FNR == NR {
	x[$1] = $2
	next
}
$1 in x {
	$2 = x[$1]
}
1' file[12]

which (with your sample files) produces the output:
Code:
cellRef 2530};disabled;main;0;"Trx4";
 cellRef 2530};"4015_3_DRONAGIRI";main;0;"Trx2";
 cellRef 2355};"4245_3_SAI_PRASAD_KOPRA";main;12;"Trx3";outer
 cellRef 765};"4116_3_MARUTI_TOWER";main;11;"Trx2";
 cellRef 580};"4344_1_CHAMUNDA_DARSHAN";secondary;0;"Trx5";inner
 cellRef 580};"4344_1_CHAMUNDA_DARSHAN";secondary;0;"Trx6";inner
 cellRef 2437};"4600_1_YOGESHWAR_CHS";main;-1;"Trx1";
 cellRef 2437};"4600_1_YOGESHWAR_CHS";main;-1;"Trx3";
 cellRef 2437};"4600_1_YOGESHWAR_CHS";main;-1;"Trx2";
 cellRef 2437};"4600_1_YOGESHWAR_CHS";main;-1;"Trx4";
 cellRef 2475};"4136_2_MTNL_APMC";main;6;"Trx3";outer
 cellRef 2305};disabled;main;6;"Trx2";
 cellRef 2368};disabled;main;0;"Trx4";outer
 cellRef 2368};disabled;secondary;0;"Trx5";inner
 cellRef 2368};disabled;secondary;0;"Trx6";inner
 cellRef 956};disabled;secondary;4;"Trx5";inner
 cellRef 956};disabled;secondary;3;"Trx6";inner
 cellRef 932};disabled;secondary;6;"Trx5";inner
 cellRef 932};disabled;secondary;5;"Trx6";inner
 cellRef 864};disabled;secondary;4;"Trx5";inner

Note that no change is made to the first line because there is a big difference between:
Code:
cellRef<space>2530}
        and
<space>cellRef<space>2530}

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-06-2014
No success.. Smilie

With /usr/xpg6/bin/awk:
Code:
input file "file[12]"

With /usr/xpg6/bin/awk
Code:
line 11: /usr/xpg6/bin/awk: No such file or directory

With nawk
Code:
nawk: can't open file file[12]
 source line number 2

With awk

Code:
awk: syntax error near line 8
awk: bailing out near line 8

---------- Post updated at 02:58 PM ---------- Previous update was at 02:56 PM ----------

Code:
uname -a
SunOS omcmfnt1 5.10 Generic_142900-08 sun4u sparc SUNW,Sun-Fire-V490

# 4  
Old 11-06-2014
You said you had two files named file1 and file2. The error messages you're seeing tell us that file1 and file2 are not present.

Did you change the names of your files? Did you decide to run this script in a directory other than the directory that contains your data files?
# 5  
Old 11-06-2014
Don't use double quotes around the filenames (i.e. file[12]) as they need to be expanded by the shell. Assuming a typo in your samples' first lines, I'd suggest "replacing field 1 in file 2 with field 2 in file 1 if both files' field 1 match":
Code:
awk -F";" 'FNR==NR{T[$1]=$2; next} $1 in T {$1=T[$1]} 1' OFS=";" file[12]
"4015_3_DRONAGIRI";disabled;main;0;"Trx4";
"4015_3_DRONAGIRI";disabled;main;0;"Trx2";
"4245_3_SAI_PRASAD_KOPRA";disabled;main;12;"Trx3";outer
"4116_3_MARUTI_TOWER";disabled;main;11;"Trx2";
"4344_1_CHAMUNDA_DARSHAN";disabled;secondary;0;"Trx5";inner
"4344_1_CHAMUNDA_DARSHAN";disabled;secondary;0;"Trx6";inner
"4600_1_YOGESHWAR_CHS";disabled;main;-1;"Trx1";
"4600_1_YOGESHWAR_CHS";disabled;main;-1;"Trx3";
"4600_1_YOGESHWAR_CHS";disabled;main;-1;"Trx2";
"4600_1_YOGESHWAR_CHS";disabled;main;-1;"Trx4";
"4136_2_MTNL_APMC";disabled;main;6;"Trx3";outer
 cellRef 2305};disabled;main;6;"Trx2";
 cellRef 2368};disabled;main;0;"Trx4";outer
 cellRef 2368};disabled;secondary;0;"Trx5";inner
 cellRef 2368};disabled;secondary;0;"Trx6";inner
 cellRef 956};disabled;secondary;4;"Trx5";inner
 cellRef 956};disabled;secondary;3;"Trx6";inner
 cellRef 932};disabled;secondary;6;"Trx5";inner
 cellRef 932};disabled;secondary;5;"Trx6";inner
 cellRef 864};disabled;secondary;4;"Trx5";inner

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 11-06-2014
Thanks Don, RudiC.. Both worked well..SmilieSmilieSmilie


I have fixed the mistakes I was making at my end related to file names..Smilie
# 7  
Old 11-06-2014
I misread what you were trying to do. To get the output you wanted using my suggestion, you'd need to change the line:
Code:
	$2 = x[$1]

to:
Code:
	$1 = x[$1]

In the future, please show us a sample of the output you're trying to produce as well as samples of your input files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

using field 2 in file2 to complete field 3 in file1

Hello, I was hoping someone could help me with this work related problem... basically what I want to do is the following: file2: 1 o 2 t 4 f 5 v 7 n 8 e 10 a file1: 1 : (8 Replies)
Discussion started by: smarones
8 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

10. Shell Programming and Scripting

replacing text in file1 with list from file2

I am trying to automate a process of searching through a set of files and replace all occurrences of a formatted text with the next item in the list of a second file. Basically i need to replace all instances of T????CLK???? with an IP address from a list in a second file. the second file is one IP... (9 Replies)
Discussion started by: dovetail
9 Replies
Login or Register to Ask a Question