Using awk to substitute columns from one file into another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to substitute columns from one file into another
# 1  
Old 05-05-2009
Using awk to substitute columns from one file into another

Hi,

I am new to this forum and my script skills are very weak.

I have this file (file 1) that contains 3 columns (tab delimited):

kyle start stop
john start stop
joe start stop

And I want to replace name (column 1) from another file. This other file has two columns, one is the match (column 1) and the other column is what I want to substitute into file 1.

kyle bob
john jack
joe arnold

So the output file would look like this:

bob start stop
jack start stop
arnold start stop


Could some give me a solution and a simple explanation. Any help would be fine Smilie I tried finding a similar thread but it looks like my case is unique.

Thanks
# 2  
Old 05-05-2009
Quote:
Originally Posted by kylle345
I tried finding a similar thread but it looks like my case is unique.
Everybody belive that Smilie but is not.
Anyway try awk
Code:
awk 'NR==FNR{_[$1]=$2;next}$1 in _{$1=_[$1]}1' OFS='\t' file2 file1 > file3

..and now that you have the solution you can search the forum for similar problems.
# 3  
Old 05-05-2009
this is not awk but alternative solution using Python
Code:
#/usr/bin/env python
d={}
for line in open("file1"):
    line=line.strip().split()
    d[line[0]]=line[-1]
for line in open("file"):
    line=line.strip().split()
    print d[line[0]],' '.join(line[1:])

output:
Code:
# ./test.py
bob start stop
jack start stop
arnold start stop

# 4  
Old 06-26-2009
hi danmero, i have the same problem, but i have delimiters as | and I have to replace 41st field in file1 with 2nd field from file2. since i do not understand much awk, i cant change your command to my need. Will you be able to help me out here.
# 5  
Old 06-29-2009
danmero, i used the following version of your commands and it works. Do tell me if you think there might be an issue with this. Also i m yet to test it with my production which is more than 600 MB in size.

nawk -F'|' 'NR==FNR{_[$1]=$2;next}$41 in _{$41=_[$41]}1' OFS='|' file2 file1 > file3

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitute from awk results

Hi, The following awk command : asmcmd lsdg | awk '{print $13;}' | grep -i ${SID} return the following output . An Empty line + two lines contain "/" at the end of the line INDEVDATA/ INDEVFRA/ I need to remove the "/" as well as the empty line. Please advise Thanks (3 Replies)
Discussion started by: Yoav
3 Replies

2. Shell Programming and Scripting

awk to substitute ip without zero left padding

Hello All, I have this script to awk IP to new file. #awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP existing.txt > new.txt When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e,... (3 Replies)
Discussion started by: Shaan_Shaan
3 Replies

3. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

4. Shell Programming and Scripting

substitute with awk

When the line contains abc, it will goes to the next line and substitue the MM to NN bc 23 33 abc 23 33 ddd MM xx dff MM 33 cat xxx |awk '{if ($0~/abc/){getline;sub(/MM/,"NN")}{print}}', It doesn't show "abc 23 33 bc 23 33 ddd NN xx dff MM 33 bc 23 33 abc 23 33 ddd NN xx... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

6. Shell Programming and Scripting

using awk to substitute data in a column delimited text file

using awk to substitute data in a column delimited text file hello i would like to use awk to do the following calculation from the following snippet. input file C;2390 ;CV BOUILLOTTE 2L 2FACES NERVUREES ;1.00 ;3552612239004;13417 ;25 ;50 ; 12;50000 ; ; ... (3 Replies)
Discussion started by: iindie
3 Replies

7. UNIX for Advanced & Expert Users

Unix Bash: substitute columns in .csv using other .csv columns

Hi All, I have two .csv's input.csv having values as (7 columns) ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006 XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010 output.csv having (25 columns) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y... (4 Replies)
Discussion started by: abhivyas
4 Replies

8. Shell Programming and Scripting

awk some columns from file

I have a file that has a list in this format: abcdefg|mia21acs.acs.oaklahoma.net|10.83.19.21|||PROV|ADTHNION21E|USA|DLAR|CISCO||OS|1.0.7.10 abcdefg|cle22acs.acs.oaklahoma.net|10.83.19.22|||PROV|ADTHNION22E|USA|DLAR|CISCO||OS|1.0.7.10 I need to pull the red highlighed fileds so the output looks... (2 Replies)
Discussion started by: numele
2 Replies

9. Shell Programming and Scripting

how to substitute filepaths with sed or awk?

I am having the following problem. I am having a lot of files (test_1_01.hea, test_1_02.hea, etc) with the content: project_directory /net/1/d_1/5/ tmp_directory /net/1/d_1/5/ material_directory /net/1/d_1/5/ And I have to substitute the filepaths with new counted ones where the... (3 Replies)
Discussion started by: ergy1983
3 Replies

10. Shell Programming and Scripting

AWK Multiple substitute

I want to reformat the following: ID1 ID001 0 0 2 1 GG TC GG CT GG AA AA AG ID2 ID002 0 0 2 2 GG 00 AG CC GG GG TC CC I want to replace only:... (1 Reply)
Discussion started by: genehunter
1 Replies
Login or Register to Ask a Question