Replace column with column from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace column with column from another file
# 1  
Old 04-13-2011
Replace column with column from another file

Hello,

I am trying to replace the column in file1 with the column from file2. The two files will have the same amount of rows. Each row will correspond with the same row in the other file.

File1

Code:
 
                                            "Replace this column"
500     13-APR-2011 09:00:00 PM       DPDP     00-00-0001234567      Stuff (500)        Stuff 500
500     13-APR-2011 09:00:00 PM       DPDP     00-00-0008912345      Things (625)       Things 625

File2

Code:
 
00001234567 
00008912345

awk is preferred.

Thanks.

Last edited by doobe01; 04-13-2011 at 05:52 PM.. Reason: Format of file1, not evenly delimtied with whitespace.
# 2  
Old 04-13-2011
Try:
Code:
awk 'NR==FNR{a[NR]=$0;next}{$6=a[FNR]}1' File2 File1

# 3  
Old 04-13-2011
Thanks. tried this and it didn't replace the $6 field. It just added the column to the begninning of each line.
# 4  
Old 04-13-2011
That is weird, because for your input files my code is working fine...
Code:
[root@linux ff]# cat File1
500 13-APR-2011 09:00:00 PM DPDP 00-00-0001234567 Stuff (500) Stuff 500
500 13-APR-2011 09:00:00 PM DPDP 00-00-0008912345 Things (625) Things 625
[root@linux ff]# cat File2
00001234567
00008912345
[root@linux ff]# awk 'NR==FNR{a[NR]=$0;next}{$6=a[FNR]}1' File2 File1
500 13-APR-2011 09:00:00 PM DPDP 00001234567  Stuff (500) Stuff 500
500 13-APR-2011 09:00:00 PM DPDP 00008912345 Things (625) Things 625

# 5  
Old 04-13-2011
That is weird. I tried three times and got the same result. Added it to the beginning...

What could be causing that?
# 6  
Old 04-13-2011
Does your input files look exactly like those in first post? What operating system are you using?
# 7  
Old 04-13-2011
Yeah, my output files look exactly like that.

We are on HPUX 11.23.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Get extract and replace column with link in a column where it exists

hi i have sample data a,b,c,d,e,g h http://mysite.xyx z,b,d,f,e,s t http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing output expected is a,b,c,d,e,http://mysite.xyx z,b,d,f,e,http://123124# a,b,c,i,m,nothing d,i,j,e,w,nothing i can get only links using grep -o 'http.*' i... (8 Replies)
Discussion started by: zozoo
8 Replies

3. Shell Programming and Scripting

Match column 8 in file 1 with column 2 in file 2 and replace..

I am looking at the NR==FNR posts and trying to use them to achieve the following but I am not getting it. I have 2 files. I want to match column 8 in file 1 with column 2 in file 2. When they match I want to replace column 9 in file 1 with column 1 in file 2. This is and extract from file 1 ... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

5. UNIX for Dummies Questions & Answers

replace a column with values from another file

Dear all, I have a file1.pdb in pdb format and a dat file2 containing values, corresponding to the atoms in the pdb file. these values (file2.dat) need to be in the column instead of the 0.00 (file1) values for each atom in file1.pdb .(the red values must be replaced by the blue ones,in order)... (11 Replies)
Discussion started by: chen.xiao.po
11 Replies

6. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

9. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies

10. Shell Programming and Scripting

Replace 10th column with a new column--- Terriblly hurry

Hi Can any one tell me how to replace the 10th column in a file(comma delimted) with a new file with a single column. Can any one Help me out of the please as soon as possible as i am in a terribley hurry!!!!!! Many THanks, (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies
Login or Register to Ask a Question