Replacing a column in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a column in a file
# 1  
Old 05-18-2011
Replacing a column in a file

Hi,

I want to replace a column in a file with a file that only has one column. Reading another post this is what I'm trying to do,

The existing file has n columns and I paste the extra column using the paste command to make it n+1

Code:
paste file1 file2 > file3

Firstly I want to now remove say column 7 from file3 and then re-arrange the columns so that the n+1 column becomes the 7th column. How would I do this using awk?

Thanks.
# 2  
Old 05-18-2011
Post a sample date(from your files) and will try to find a solution.
# 3  
Old 05-18-2011
If File1 is the file with one column, this should replace the 7th column of File2 with the column of File1.

Code:
awk 'NR==FNR{a[NR]=$0; next} {$7=a[FNR]}1' File1 File2

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-18-2011
Thanks! Works like a charm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a column in a pipe delimited file

Hi, I have a pipe delimited file as below and I need to replace the 2nd column of each line with null values. 1|10/15/2011|fname1|lname1 2|10/15/2012|fname2|lname2 3|10/15/2013|fname3|lname3 Output file: 1||fname1|lname1 2||fname2|lname2 3||fname3|lname3 I tried this ... (2 Replies)
Discussion started by: member2014
2 Replies

2. Shell Programming and Scripting

Replacing a column in a file with a value

Hi , Could anyone help me for the below request . Suppose i have the data like below : 00006139 2066522 DURTS S 0 20121219 20121219 20121120 00006137 2515401 DURTS S 0 20121219 20121219 20121120 00004363 6030777 DURTS S 0 20121219 20121219 ... (2 Replies)
Discussion started by: Ravi Tej
2 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: digipak
4 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Replacing a column in a text file

Say I had a text file that contained four columns, like the following: Mack Christopher:237 Avondale Blvd:970-791-6419:S Ben Macdonor:30 Dragon Rd:647-288-6395:B I'm making a loop that will replace the fourth column a line in the file with the contents of a variable 'access', but I have no... (6 Replies)
Discussion started by: Sotau
6 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