Replacing column with another column of different file by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing column with another column of different file by awk
# 1  
Old 09-28-2010
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
Code:
qq    ww   rr   tt   ee  ff
qq    ww   rr   tt   ee  ff

Second file format text2.txt
Code:
aa
aa

Now o/p I want as text1.txt's 4th column replaced by 1st column of second file
Code:
qq    ww   rr   aa   ee  ff
qq    ww   rr   aa   ee  ff

Please help me to solve this

Moderator's Comments:
Mod Comment Having 34 posts you should be familiar using code tags - please do so next time!
# 2  
Old 09-28-2010
Try this.

Code:
[muthuk@nexthallwhen-dx tmp]$ cat file.txt
qq    ww   rr   tt   ee  ff
qq    ww   rr   tt   ee  ff
[muthuk@nexthallwhen-dx tmp]$ cat file1.txt
aa
aa
[muthuk@nexthallwhen-dx tmp]$ paste file.txt file1.txt | awk '{ print $1"\t"$2"\t"$3"\t"$NF"\t"$5"\t"$6; }'
qq      ww      rr      aa      ee      ff
qq      ww      rr      aa      ee      ff

It will help out.
# 3  
Old 09-28-2010
Or:
Code:
awk 'NR==FNR{a[NR]=$0;next} {$4=a[FNR]}1' OFS="\t" test2.txt test1.txt

# 4  
Old 09-28-2010
Quote:
Originally Posted by kmuthu_gct
Try this.

Code:
[muthuk@nexthallwhen-dx tmp]$ cat file.txt
qq    ww   rr   tt   ee  ff
qq    ww   rr   tt   ee  ff
[muthuk@nexthallwhen-dx tmp]$ cat file1.txt
aa
aa
[muthuk@nexthallwhen-dx tmp]$ paste file.txt file1.txt | awk '{ print $1"\t"$2"\t"$3"\t"$NF"\t"$5"\t"$6; }'
qq      ww      rr      aa      ee      ff
qq      ww      rr      aa      ee      ff

It will help out.
a variant:
Code:
paste f1 f2 |  awk '{$4=$7;$7=""}1' OFS="\t"

# 5  
Old 09-28-2010
Code:
$ ruby  -ane 'BEGIN{a=[];b=File.read("file2").split}; $F[3]=b[$.-1]; puts $F.join(" ")' file
qq ww rr aa ee ff
qq ww rr aa ee ff

# 6  
Old 09-28-2010
Code:
awk '{t=$0;getline<"test1.txt";$4=t}1' test2.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching column value from 2 different file using awk and append value from different column

Hi, I have 2 csv files. a.csv HUAWEI,20LMG011_DEKET_1296_RTN-980_IDU-1-11-ISV3-1(to LAMONGAN_M),East_Java,20LMG011_DEKET_1296_RTN-980_IDU-1,20LMG011,20LMG 027_1287_LAMONGAN_RTN980_IDU1,20LMG027,1+1(HSB),195.675,20LMG011-20LMG027,99.9995,202.6952012... (7 Replies)
Discussion started by: tententen
7 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

4. Shell Programming and Scripting

Compare the second column of a file with the second column of another in awk

Hi, I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem. I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields): example... (5 Replies)
Discussion started by: amarn
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

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

9. UNIX for Advanced & Expert Users

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies

10. UNIX for Dummies Questions & Answers

Replacing column with column of another file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8 spaces>> ... (1 Reply)
Discussion started by: manneni prakash
1 Replies
Login or Register to Ask a Question