Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting


Tags
awk file matching replace, solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
ailnilanjan's Unix or Linux Image
ailnilanjan ailnilanjan is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 22 April 2017, 11:17 AM EDT
Posts: 48
Thanks: 25
Thanked 0 Times in 0 Posts
Replacing first field of file2 with the second filed of file1 for matching cases

Dear All,

Need your help..Linux

I am not regular on shell scripts..Linux

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..
Sponsored Links
    #2  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 April 2018, 3:27 PM EDT
Location: San Jose, CA, USA
Posts: 11,206
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
ailnilanjan (11-06-2014)
Sponsored Links
    #3  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
ailnilanjan's Unix or Linux Image
ailnilanjan ailnilanjan is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 22 April 2017, 11:17 AM EDT
Posts: 48
Thanks: 25
Thanked 0 Times in 0 Posts
No success.. Linux

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 Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 April 2018, 3:27 PM EDT
Location: San Jose, CA, USA
Posts: 11,206
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
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?
Sponsored Links
    #5  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 April 2018, 4:52 PM EDT
Location: Aachen, Germany
Posts: 12,492
Thanks: 401
Thanked 3,871 Times in 3,559 Posts
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

The Following 2 Users Say Thank You to RudiC For This Useful Post:
ailnilanjan (11-06-2014), Don Cragun (11-06-2014)
Sponsored Links
    #6  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
ailnilanjan's Unix or Linux Image
ailnilanjan ailnilanjan is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 22 April 2017, 11:17 AM EDT
Posts: 48
Thanks: 25
Thanked 0 Times in 0 Posts
Thanks Don, RudiC.. Both worked well..LinuxLinuxLinux


I have fixed the mistakes I was making at my end related to file names..Linux
Sponsored Links
    #7  
Old Unix and Linux 11-06-2014   -   Original Discussion by ailnilanjan
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 April 2018, 3:27 PM EDT
Location: San Jose, CA, USA
Posts: 11,206
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
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.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare file1 for matching line in file2 and print the difference in matching lines RasB15 Shell Programming and Scripting 2 11-07-2013 09:04 AM
Pattern Matching & replacing of content in file1 with file2 siramitsharma Shell Programming and Scripting 4 03-09-2013 05:59 AM
if matching strings in file1 and file2, add column from file1 to file2 pathunkathunk UNIX for Dummies Questions & Answers 3 07-19-2012 11:02 PM
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2 right_coaster Shell Programming and Scripting 4 10-06-2011 06:07 PM
replacing text in file1 with list from file2 dovetail Shell Programming and Scripting 9 07-24-2008 01:20 AM



All times are GMT -4. The time now is 07:22 PM.