Replacing column 1 in one file with values in other file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing column 1 in one file with values in other file
# 1  
Old 06-11-2011
Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following;
File-1:

ABCDW01 12322 23322
BDADW01 22232 24453
EDFAW00 32232 23422
and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a particular order.

File-2:
EDFAW00 XXWY_11_1
BDADW01 UHBS_100_0
ABCDW01 CATS_200_1
and so on.

I want to replace in the first file entries in column 1 with their acutal values in file 2, so essentially, this is what I want to achieve

Result Required;

CATS_200_1 12322 23322
UHBS_100_0 22232 24453
XXWY_11_1 32232 23422

This is not a homework.
Thanks for any help in advance.
# 2  
Old 06-11-2011
Code:
awk 'NR==FNR{a[$1]=$2;next}{$1=a[$1]}1' file2 file1

# 3  
Old 06-11-2011
It works, thanks a million
# 4  
Old 06-11-2011
Hi.

Also with standard utilities:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate use of standard utilities sort and join.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sort join

pl " Results of standard GNU sort and join:"
join -o 2.2 1.2 1.3 <( sort data1 ) <( sort data2 )

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
sort (GNU coreutils) 6.10
join (GNU coreutils) 6.10

-----
 Results of standard GNU sort and join:
CATS_200_1 12322 23322
UHBS_100_0 22232 24453
XXWY_11_1 32232 23422

See man and info pages for details ... cheers, drl
# 5  
Old 06-12-2011
Thanks DRL, excllent tutorial above solution is. Thanks a lot God Bless.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing values inside a file.

Good day guys, I'm having trouble in creating a logic when it comes to replacing the values inside a file. I tried using sed command but it just doesn't work the way I want it to be. Here is what I'm trying to achieve. If my input file contains the values below. NAME++GUEST1 ++GUESS2++... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

Replacing column values

hi all , ( perl) i am trying to replace jst one column in a file for eg month dayofweek dealar car-name jan thurs mercedes c300 feb wed lexus is300 all this data is in a master file and i want to replace jan with 1 feb... (5 Replies)
Discussion started by: technoman
5 Replies

5. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

6. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

7. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

Replacing column with another column of different file by awk

Hi All, I will really appreciate if you kindly lookinto my requirement below and provide me a solution First file format test1.txt qq ww rr tt ee ff qq ww rr tt ee ff Second file format text2.txt aa aa Now o/p I want as text1.txt's 4th column replaced... (5 Replies)
Discussion started by: Pratik4891
5 Replies

9. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

10. Shell Programming and Scripting

Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script. 1) Replacing any spaces in the file with a ~ tr ' ' '~' <$orignalFile> $newFile 2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces tr ' ' '~' <$newFile> $newFile2 3) Last... (4 Replies)
Discussion started by: hgjdv
4 Replies
Login or Register to Ask a Question