Replacement code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacement code
# 1  
Old 08-05-2014
Replacement code

Hello everybody,

I am new to this forum and i need some input for the below issue


I have 3 files

file 1

Code:
a  mumbai    32344
m  bangalore 4211
x  delhi          4345
e  chennai     4312
d  punjab       5255



file 2

Code:
b
t
j
c
d



data in file 2 will be compared with column 1 of file 1 and if it matches data of file3 will be replaced with the data in column 2 of file2

file 3

Code:
kolkatta
nagpur
goa
chennai
punjab

Thanks,
New Shellscripter
# 2  
Old 08-05-2014
Hi New Shellscripter,

provide me your expected output..
# 3  
Old 08-05-2014
Thank you Bharat for the quick reply.

Let me frame the question once more with a new dataset with the expected output

Hello everybody,

I need a help on this.
I have a 3 files

file 1
Code:
a  mumbai    32344
m  bangalore 4211
c delhi          4345
e  chennai     4312
d  punjab       5255

file 2
Code:
a
t
j
c
d

data in file 2 will be compared with column 1 of file 1 and if it matches data of file3 will be replaced with the data in column 2 of file2

file 3
Code:
kolkatta
nagpur
goa
chennai
haryana

expected output :-

Code:
a  kolkatta      32344
m bangalore  4211
c  chennai     4345
e  chennai     4312
d   haryana      5255

# 4  
Old 08-05-2014
Your file2 has no column 2 that could be used for the replacement...?
# 5  
Old 08-05-2014
data in file 2 will be compared with column 1 of file 1, and if it matches, data of file3 will be replaced with the data, in column 2 of file1

I hope i am able to make some sense now.
# 6  
Old 08-05-2014
Try:
Code:
awk 'NR==FNR{getline v<f; A[$1]=v; next} $1 in A { $2=A[$1] }1' f=file3 file2 file1

or
Code:
paste file2 file3 | awk 'NR==FNR{A[$1]=$2; next} $1 in A { $2=A[$1] }1' - file1

or if with bash/ksh93/zsh you could also use::
Code:
awk 'NR==FNR{A[$1]=$2; next} $1 in A { $2=A[$1] }1' <(paste file2 file3) file1

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 08-05-2014
Thank you very Scrutinizer for the help.
Code works for me.

It would be highly helpful if you can explain a bit of what is done.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Is this MB, which needs replacement ?

Hello, I am getting below error in fmadm output. This server is not in support, so can't reach them. Is it showing that motherboard is faulty and should be replaced ? It was rebooted a week back and then, there were no errors # fmadm faulty --------------- ------------------------------------ ... (1 Reply)
Discussion started by: solaris_1977
1 Replies

2. Shell Programming and Scripting

Perl:: mass replacement of converting C code formats to tgmath.h

hello, i have a lot of C old code I'm updating to C11 with tgmath.h for generic math. the old code has very specific types, real and complex, like cabsl, csinhl, etc usually for simple bulk replacements i would do something simple like this perl -pi -e 's/cosl/cos/g' *.c the reference... (0 Replies)
Discussion started by: f77hack
0 Replies

3. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

4. UNIX for Dummies Questions & Answers

replacement

my filename.txt looks like this: 2079951061790 SCK0000891539000000000000021600R 2079951061790 SCK0000901487000000000000028900R 2079951061790 SCK0000903092000000000000021300R 2079951074758 ... (9 Replies)
Discussion started by: tjmannonline
9 Replies

5. Shell Programming and Scripting

help me :replacement

Hi pls help me for below; i have a file .content is : =================== uid,pcsPricingPlan,refPcsQosProfName 821910002022,smartlimit,SGSNQOS1 i have to replace the value of uid and pricingplan by a unix script. may be the value would be next line or any where in the file. pls... (9 Replies)
Discussion started by: Aditya.Gurgaon
9 Replies

6. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

7. UNIX for Dummies Questions & Answers

Pattern Replacement

There is a requirement that i need to replaced a pattern by another pattern in all the files in my entire file system. there are 1000s of file in the system. let the pattern is "calcuta". i have to replace this pattern by "kolkata" in all those files which contain "calcuta". I am only able to... (12 Replies)
Discussion started by: palash2k
12 Replies

8. UNIX for Dummies Questions & Answers

Regarding Replacement

I have two files: file1: somedata <html> <head> This is sample statement ...... ...... </head> </html> somedata file2: olga 81 91 B A rene 82 92 B A zack 83 93 Expextd Result: (2 Replies)
Discussion started by: rajx
2 Replies

9. UNIX for Advanced & Expert Users

Replacement for cron ?

Hi, We are running pre and post processing scripts (master server initiated) for backups (Veritas Netbackup Datacentre) - So our policies and schedules are kicked off via cron - however, we have so many servers, policies and schedules that managing these in cron/crontab is a nitemate. Are... (3 Replies)
Discussion started by: B-Man
3 Replies

10. UNIX for Dummies Questions & Answers

Replacement for netinfo

We are getting ready to migrate away from our netinfo server with is old Open Step. We were thinking of starting to use LDAP for all account creations, passwords... Does anyone have any pros or cons against this or what you are using? Thanks for the help. (0 Replies)
Discussion started by: rickyt00
0 Replies
Login or Register to Ask a Question